Search code examples
javascripthashbrowser-history

Can I be sure the page won't reload if I use `location.replace` to change hash?


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)?


Solution

  • 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);