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

后端 未结 4 2179
没有蜡笔的小新
没有蜡笔的小新 2020-12-14 00:06

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 t

4条回答
  •  难免孤独
    2020-12-14 00:43

    Another option is to remove the hash and store it in session storage to be retrieved on reload:

    var newUrl = location.href + '#myHash';
    var splitUrl = newUrl.split('#');
    newUrl = splitUrl[0];
    if (splitUrl[1]){
        sessionStorage.savedHash = splitUrl[1];
    }
    location.href = newUrl;
    

    and then on top of your page you can have the following code:

    var savedHash = sessionStorage.savedHash;
    if (savedHash){
        delete sessionStorage.savedHash;
        location.hash = savedHash;
    }
    

提交回复
热议问题