Need jquery 'if' to avoid altering hrefs that include #

Ok, this is a follow up of a question answered by chaau yesterday, but it is open to anyone of course.

I needed to alter the urls on a page so that logged in users get the link to live (not cached) pages when navigating my site.

chaau put this together for me:

$("a").prop("href", function(){
  return $(this).prop("href") + "?cache=n";
});

and I use it in my header file like this:

---------------------------------
<script>
if (document.cookie.indexOf("cachen") >= 0) {
$("a").prop("href", function(){
  return $(this).prop("href") + "?cache=n";
});
}
</script>
--------------------------------

(the 'cachen' cookie is set for all logged in users).

I found a small problem though, I have some content in javascript tabs on the site. To select a tab, a user clicks on a url like: http://www.mysite.com/#tab1

Adding the '?cache-n' to that causes the tab to fail.

Is  there a way (and can you show me how) to change this script so that appending the  '?cache=n'  doesn't apply to links that include a # ?

Thanks!

Chris
St_Aug_Beach_BumAsked:
Who is Participating?

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

x
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.

Hans LangerCommented:
Try just validating if it contains a hash and add the hash at the end of the url:
$("a").prop("href", function(){
  var url =  $(this).prop("href")
  if(url.indexOf('#')>-1){
    return url.split('#')[0] + "?cache=n#" + url.split('#')[1];
  }else{
    return url + "?cache=n";
  }
  
});

Open in new window

Julian HansenCommented:
Why not give your links you want to target a a class that you can then do this to

$("a.convert").prop(...

Open in new window


That way you can support all kinds of links and still select only those ones you need to have the suffix appended to.

Other than that just do as Hans Langer says - although I am not sure why he wants to append the '#' to the end of the URL - I would just look for an instance of '#' in the string and if it is found simply  return the URL.

Something like this
$(function() {
	$("a").prop("href", function(){
		var url =  $(this).prop("href")
		if(url.indexOf('#') == -1){
			return url + "?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
Hans LangerCommented:
Well, he is adding "cache=n" in the url to avoid the cache, but the hash could be used by some logic inside the page, thay why I did not want to remove it.
St_Aug_Beach_BumAuthor Commented:
Thank you both for chipping in here.

I look at and tried these and I am going to use:

$(function() {
      $("a").prop("href", function(){
            var url =  $(this).prop("href")
            if(url.indexOf('#') == -1){
                  return url + "?cache=n";
            }
      });
});

This is a bit simpler and does not remove the #, and applying to classes is a good idea except that there are numerous links from different files involved. And this works well just skipping these ones.

Thanks again,

Chris
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.