Imagine the following markup:
<a href="#link01">1</a>
<a href="#link02">2</a>
And:
$('a').click(function(){
var href = $(location).attr('href'),
hrefLenght = href.length,
hrefPos = href.indexOf("#"),
hrefPage = href.slice(hrefPos+1);
alert(hrefPage);
});
Now, what happens after you click a link is that jQuery alerts the hrefPage
which was there before you clicked the link, not after, as expected. Therefore:
1) I'm located at http://www.example.com/#link01
2) I click a link
3, result) link01
is echoed
3, expected) link02
is echoed
Any ideas how to fix this?
EDIT: Why am I using location instead of href? Because I want the aforementioned code to work also when the page loads with the href without anything having been clicked at all and I don't think it's a good idea to write two pieces of code for the same process?