Javascript 如何在javascript中捕获“History.Back”事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4967414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:00:19  来源:igfitidea点击:

How to catch "History.Back" event in javascript?

javascriptjavascript-events

提问by maycil

is it posible to catch "History.Back" event in javascript? ? want to catch history.back() event and logout user.

是否可以在 javascript 中捕获“History.Back”事件?? 想要捕捉 history.back() 事件并注销用户。

P.s : There is some code about disabling history.back(). But is not useful for me.

Ps :有一些关于禁用 history.back() 的代码。但是对我没用。

<script type = "text/javascript" >
 function disableBackButton()
 {
   window.history.forward();
 }
 setTimeout("disableBackButton()", 0);
</script>

then set onunloadevent

然后设置onunload事件

<body onunload="disableBackButton()">

回答by gox

You can catch history back using "window.onpopstate" event. But AFAIK it is only available in HTML5.

您可以使用“window.onpopstate”事件来捕捉历史记录。但 AFAIK 它仅在 HTML5 中可用。

http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#event-definitions-0

http://www.whatwg.org/specs/web-apps/current-work/multipage/history.html#event-definitions-0