javascript or jquery to append variable to all urls on page


I need to append a variable to all the <a href> urls on my site if the user is logged in.

If users are logged in, there is a cookie set.  So right now:

1)  I check for the cookie
2) check to see if the variable is already set (so I don't get a redirect loop)
3) Now I need to append "?cache=n" to every url in a href.

It looks like this is possible, I found an example code at  that does something similar, but not exactly, to what I want.

Here's what I've got so far (steps 1 and 2, but they might need some attention too):

if( document.cookie.indexOf('cachen') >= 0) {
      var cachevar;
       var cachevar =;
if(! cachevar){       

//now add something to append "?cache=n" to all the urls on the page


I can use javascript or jquery (version 1.8.1 is loaded on the site), but I am not real familiar with either.  Can you show me how to do this?


Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

To select all <a> elements you need to use the element selector. To set the href property you need to use prop():
$("a").prop("href", function(){
  return $(this).prop("href") + "?cache=n";

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
St_Aug_Beach_BumAuthor Commented:
Works great - thank you!
St_Aug_Beach_BumAuthor Commented:

If you don't mind and have a moment, I have a follow up question to this one...'if'-to-avoid-altering-hrefs-that-include.html

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.