質問

I am using hash in order to avoid reloading the page (yeah, I know there's a new history API, but whatever...)

But now I want to change the hash without adding it to session History (i.e. don't want it in back button list), or add it removing the previous one. So what I don't want is having both the new one and old one in the list.

It seems I can use location.replace:

location.replace( location.href.replace(/#.+/, '') + '#' + newHash );

On Firefox 29 it works well and, since I have changed only the hash, the page isn't reloaded.

But does it work like this on all browsers (IE, I'm looking at you)?

役に立ちましたか?

解決 2

After testing (test page) with browserling and browserstack, it seems that

  • No browser reloads the page
  • Some browsers don't replace the last item in back button list.

Specifically, my code works on

  • Firefox: 3
  • IE: 6
  • Chrome: 6 (but not on 5)
  • Safari: 5.0.5 (but not on 4.0)
  • Opera: 12.10 (but not on 11.6)

And, according to IETester, it works on IE 5.5 too.

Edit - The following code also works (test page), with same browser support:

location.replace('#' + newHash);

他のヒント

Yes, you can be sure. Why would the replace method do the same thing as setting window.location to a url? If I were you, I would use window.location.hash = "#newhash".

LE: Or just call history.replaceState(). It is the same as pushState, the only difference being that this method does not create a new history entry.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top