Michael Schmidt
Michael Schmidt

Reputation: 9230

Change hashtag before call javascript function

I have a simple html link: <a href="#about">.
The current URL is http://www.example.com/whatever.html#info.
And I have a simple jQuery code:

$('a').click(function(){
   alert(window.location.hash);
});

Now, when I click on the link, it display me the old hashtag (info). But I want, when I click on the link then it should display me the new hashtag (about).

Is there a easy way to change the value of the hashtag before I call the jQuery code?

Note I: This is just simplification of my target. I know it doesn't make any sense, but I need it.
Note II: Because of the proxy settings of my employer, I can't serve you a jsfiddle

Upvotes: 1

Views: 428

Answers (3)

vlio20
vlio20

Reputation: 9295

If I understand you right you want to change the text in the a tag. You can do this:

$('a').click(function(){
   $(this).attr('href','#about');
   alert(window.location.hash);
});

this way you will change the href from #info to #about. Hope this is what you looked for.

Upvotes: 0

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Try using $(this).attr('href') to get the link tag's href.

$('a').click(function(){
   alert($(this).attr('href'));
});

and $(this).attr('href').substring(1) should return you about without #

DEMO: http://jsfiddle.net/nhPn5/

Upvotes: 2

SLaks
SLaks

Reputation: 887195

You can call setTimeout to run your code in a later tick of the event loop, after the browser has performed the navigation:

setTimeout(function() {
    ...
}, 0);

Upvotes: 0

Related Questions