-1

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?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Andrea Carta
  • 33
  • 1
  • 5
  • 1
    The problem is that the page is not actually changing. You will need to execute the `document.write()` functionality again instead of relying on the page reload to execute it for you. – ChrisG May 25 '21 at 15:27
  • 1
    Tangential, but `document.write()` has been somewhat obsolete for some time now - you should probably leverage more proper DOM manipulation methods. [Further reading](https://stackoverflow.com/questions/12574098/is-document-write-actually-deprecated) – esqew May 25 '21 at 15:28
  • Document.write was just a way to find what the problem was. What I need is to identify the correct hash, as in the original link. – Andrea Carta May 25 '21 at 15:37
  • @AndreaCarta `window.location.href` *does* identify the correct hash. It's just that your code is not executed again when navigating inside the page. – Bergi May 26 '21 at 00:14
  • @Bergi: and when should I execute again the code? ChrisG said the same thing too. – Andrea Carta May 26 '21 at 00:40
  • @AndreaCarta Either use a [`hashchange` event](https://developer.mozilla.org/en-US/docs/Web/API/Window/hashchange_event) handler, or a `click` handler on each of the links. And make sure not to use `document.write` in the event handler but some other DOM manipulation method (or `console.log` if you just want to output something for testing). – Bergi May 26 '21 at 00:44
  • @Sergi: the problem is subtle. The page that is not actually changing contains some script that should be executed each time the link is clicked. But this script is only executed when the page is loaded or reloaded, and changing the hash doesn't do that (I will try anyway the hashchange event). So either there's a way to force the reloading when the link is clicked - even a browser setting, something like "no cache" ever - or I've to find a way to have this code executed from outside the frame, after the link has been clicked - not sure the code will do what's supposed to do, in that case. – Andrea Carta May 27 '21 at 20:15
  • 1
    @Bergi: I stand corrected. Against my expectations the hashchange event handler did indeed work, and the code (after having been moved inside the handler) now works correctly). Yours was a very good suggestion. – Andrea Carta May 27 '21 at 20:57

1 Answers1

0

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>
  • this only prints the location.href of **the page with the link**, inside **the page with the link**. I need the location.href of **the page linked**, from inside **the page linked**. – Andrea Carta May 25 '21 at 17:41