jQuery 按下后退按钮时的jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17628612/
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
Jquery when back button is pressed
提问by user1721135
I have a click that changes things on the page when you click it. I was wondering if there was a way to trigger an effect when you press the back button on the browser
我有一个点击,当您点击它时,页面上的内容会发生变化。我想知道当你按下浏览器上的后退按钮时是否有办法触发效果
回答by user1721135
You can sort of do it using the popstate event:
您可以使用 popstate 事件来完成它:
window.onpopstate = function() {
alert("pop!");
}
or in jQuery:
或在jQuery 中:
$(window).on('popstate', function(event) {
alert("pop");
});
However this will also trigger when navigating forward, not only backward.
但是,这也会在向前导航时触发,而不仅仅是向后导航。
回答by Sakthi Karthik
Disable back url
禁用返回 url
$(function() {
if (window.history && window.history.pushState) {
window.history.pushState('', null, './');
$(window).on('popstate', function() {
// alert('Back button was pressed.');
document.location.href = '#';
});
}
});
回答by iamitpkumar
You can simply fire the “popState” event in JQuery e.g:
您可以简单地在 JQuery 中触发“popState”事件,例如:
$(window).on('popstate', function(event) {
alert("pop");
});
This is enough for what you asked … but here are few add on window event to go with…
这足以满足您的要求……但是这里很少添加窗口事件……
$(window).on('pushstate', function(event) {
alert("push");
}); // This one pushes u to forward page through history...
window.history.replaceState(); //work exactly like pushstate bt this one replace the history instead of creating new one...
window.history.backward(); // To move backward through history, just fire this event...
window.history.forward();// To move forward through history ...
window.history.go(-1); // Go back to one page(or whatever value u passed) through history
window.history.go(1); // Go forward to one page through history..
回答by Alok Kumar Kasgar
use this code for detecting browser back button event
使用此代码检测浏览器后退按钮事件
<script>
if (performance.navigation.type == 2) {
alert("Back button clicked");
}
</script>