Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

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.

share|improve this answer
    
Oh, of course! That makes perfect sense, thank you. –  NReilingh Dec 22 '14 at 0:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.