1
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ Oh, of course! That makes perfect sense, thank you. \$\endgroup\$
    – NReilingh
    Commented Dec 22, 2014 at 0:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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