javascript or jquery to append variable to all urls on page

Hi,

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  http://jsfiddle.net/6U749/  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):

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

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

}}
</script>

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?

Thanks,

Chris
St_Aug_Beach_BumAsked:
Who is Participating?
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.

chaauCommented:
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

0

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!
0
St_Aug_Beach_BumAuthor Commented:
Chaau,

If you don't mind and have a moment, I have a follow up question to this one...  http://www.experts-exchange.com/questions/28687976/Need-jquery-'if'-to-avoid-altering-hrefs-that-include.html

Chris
0
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
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.