Jump to content

Append To URL


tomtimms

Recommended Posts

Unfortunately I don't believe you can append that easily to a URL. Well, it's still easy but not like that.

 

$('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data);

 

What is 'q'? You're using jQuery correct?

Link to comment
Share on other sites

So I got it to work, however I am using a button to save and each time I save it keeps adding what I want to the end, I just need one instance of it.

 

So code $('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data); create http://www.mysite.com?a=100 and each time I click save it keeps adding 100, so it now looks like www.mysite.com?a=100100100 etc.  How can I make it just post 1 time?

Link to comment
Share on other sites

jQuery's great for chaining and slapping a whole bunch of stuff together in one line, but in this particular instance, you're probably better off breaking out a var first, resetting it to 0 or null and then setting the value.  Your current code is taking the existing value and adding to it.  Click it again and it takes the NEW value and adds to it.  Click it again....etc.

 

Take the value that's being increased accidentally, set it as its own var and reset it before appending it to the url.  Should work. 

Link to comment
Share on other sites

Depending on how you do it, you could for instance have the function bound to the click event of the button. For example:

$('#button').click(function(){
   $('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data);

   // Now unbind the click event.
   $(this).unbind('click');
});

 

Or you could simply check to see if the URL already has ?a=100 at the end of it by using .indexOf()

if($('#billing_address_setting').attr('href').indexOf(data) == -1){

  $('#billing_address_setting').attr('href', $('#billing_address_setting').attr('href') + 'site_id' + data);

}[/code]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.