# Appending URL parameters to links

I've got some pages that may or may not load with a ReturnUrl parameter, and some links that will be generated without that parameter (though I've applied a class to them).

The site uses jQuery, and I think I've found the most elegant and least breaky solution as follows:

$(document).ready(function() {$('a.my_returnlink').each(function(){
$(this).attr('href',$(this).attr('href')+window.location.search);
});
});


That is, given a page url like http://example.com/page.aspx?ReturnUrl=blahblahblah and the following link on the page: <a class="my_returnlink" href="http://example.com/someotherpage.aspx">Some link</a>, that link will end up pointing to http://example.com/someotherpage.aspx?ReturnUrl=blahblahblah.

Is there any reason not to do it this way?

Generally speaking, doing something with jQuery will perform worse than doing that same thing directly through the DOM. In this case, using the following code inside the each will yield better performance:
this.href += window.location.search;

Note, however, that code will create an invalid URL if there is already a query on the original href.