问题
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