How to force a page to reload if all what was changed in url is hash?

29,473

Solution 1

Remove the anchor you're going to navigate to, then use approach #2? Since there's no anchor, setting the hash shouldn't scroll the page.

Solution 2

I had a JQuery function that fired on $(document).ready() which detected if there was a hash appended to the URL in my case, so I kept that function the same and then just used a force reload whenever a hash change was detected:

$(window).on('hashchange',function(){ 
    window.location.reload(true); 
});

Then my other function -

$(document).ready(function() {
    var hash = window.location.hash;    
    if(hash) {
           //DO STUFF I WANT TO DO WITH HASHES
    }
});

In my case, it was fine for UX -- might not be good for others.

Solution 3

It should be expected that #foo will scroll to the anchor of the id, "foo". If you want to use approach #1 and have it reload, this approach might work.

if (Object.defineProperty && Object.getOwnPropertyDescriptor) { // ES5
    var hashDescriptor = Object.getOwnPropertyDescriptor(location, "hash"),
    hashSetter = hashDescriptor.set;
    hashDescriptor.set = function (hash) {
        hashSetter.call(location, hash);
        location.reload(true);
    };
    Object.defineProperty(location, "hash", hashDescriptor);
} else if (location.__lookupSetter__ && location.__defineSetter__) { // JS
    var hashSetter = location.__lookupSetter__("hash");
    location.__defineSetter__("hash", function (hash) {
        hashSetter.call(location, hash);
        location.reload(true)
    });
}
Share:
29,473
user2012801
Author by

user2012801

Open source projects: Java HTML compressor/minifier jQuery loading mask plugin Chrome extensions: A whole bunch Feel free to contact me at: [email protected]

Updated on March 17, 2020

Comments

  • user2012801
    user2012801 over 4 years

    I am trying to reload current page with different url hash, but it doesn't work as expected.

    (Clarification how I want it to work: Reload the page and then scroll to the new hash.)

    Approach #1:

    window.location.hash = "#" + newhash;
    

    Only scrolls to this anchor without reloading the page.

    Approach #2:

    window.location.hash = "#" + newhash;
    window.location.reload(true);
    

    Kinda works but it first scrolls to the anchor, then reloads the page, then scrolls to the anchor again.

    Approach #3:

    window.location.href = window.location.pathname + window.location.search + "&random=" + Math.round(Math.random()*100000) + "#" + newhash;
    

    Works but I would rather not add random garbage to the url.

    Is there a better solution?

  • user2012801
    user2012801 over 14 years
    At first I didn't understand that you mean removing it from DOM. It works, thanks.
  • Eli Grey
    Eli Grey over 14 years
    I never tested it but it should work in every major browser (not counting IE <8) and if it doesn't it's most likely because IE won't allow redefining the descriptor of location.hash for "security reasons" though that's bull
  • ecmanaut
    ecmanaut over 11 years
    If you want to keep the display more intact, just removing its name or id attribute should be enough for these purposes - the node itself can stay.
  • JoeBrockhaus
    JoeBrockhaus over 9 years
    jQuery's ready event will not fire when window.location = "#myHash" is executed, which would be the point in this question, I believe. However, this code is useful for loading dynamic content, for instance, if the page is reloaded.