window.history.back() Navigation Issue on Firefox?

痞子三分冷 提交于 2021-01-29 14:55:48

问题


I am currently using a script containing window.history.back() on my site. While it generally works, it occasionally jumps at times, and sometimes navigates back 2 pages instead of 1. Is there a better, or more up-to-date way to implement this code? I've currently placed it inside the <head> tag.

<script type="text/javascript">

window.addEventListener("pageshow",function(event){
    var historyTraversal=event.persisted||
    (typeof window.performance!="undefined"&&
    window.performance.navigation.type===2);
    if(historyTraversal){
        window.history.back();
    }
});

</script>

To note, I've tried implementing window.history.go(-1) in place of window.history.back(), but received the same "jumpy" result.

Also, this is the site I am currently referencing: https://www.testblog123098.tumblr.com

The browser back functionality can be tested there live.

UPDATE:

<script type="text/javascript">

window.addEventListener("load",function(event){
    var historyTraversal=event.persisted||
    (typeof window.performance!=="undefined"&&
    window.performance.navigation.type===2);
    if(historyTraversal){
        window.history.back();
    }
});

</script>

Changing pageshow to load did the trick for Chrome and Safari. Still encountering some snags in Firefox...

来源:https://stackoverflow.com/questions/63783447/window-history-back-navigation-issue-on-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!