Hash refresh in Javascript

148 Views Asked by At

I'm almost done with my previous issue, but now I'm dealing with something more subtle and annoying.

I've got a page with some links like these ones:

<a href = "page.html#1">First content</a>
<a href = "page.html#2">Second content</a>

In the page page.html there is some javascript, something like this:

document.write (window.location.href);

So, I click on the first link, and I see (in a different frame) the page "page.html", correctly starting at anchor #1. At the end the javascript writes "page.html#1", and this is correct. Then I click on the second link, and I see the page "page.html" changing its starting point, now correctly set at anchor #2. But at the end of the page I still see "page.html#1", not "page.html#2" as I expected. I think this happens because the page is exactly the same as before, only the starting point changed, so the "location.href" was not changed, despite a difference in the hash.

Is there a way to solve the problem, and get the original location.href, the one with the correct hash?

1

There are 1 best solutions below

1
On

use onclick event attribute:

<a href="page.html#1" onclick="document.write(window.location.href);">First content</a>
<a href = "page.html#2" onclick="document.write(window.location.href);">Second content</a>