当您单击它时,我单击它会更改页面上的内容。我想知道当您按下浏览器上的后退按钮时是否有办法触发效果
user2568107
问问题
69380 次
5 回答
28
您可以使用 popstate 事件进行排序:
window.onpopstate = function() {
alert("pop!");
}
或在jQuery中:
$(window).on('popstate', function(event) {
alert("pop");
});
然而,这也会在向前导航时触发,而不仅仅是向后。
于 2013-07-13T09:07:48.580 回答
10
禁用返回网址
$(function() {
if (window.history && window.history.pushState) {
window.history.pushState('', null, './');
$(window).on('popstate', function() {
// alert('Back button was pressed.');
document.location.href = '#';
});
}
});
于 2016-04-22T06:45:52.623 回答
3
使用此代码检测浏览器后退按钮事件
<script>
if (performance.navigation.type == 2) {
alert("Back button clicked");
}
</script>
于 2020-01-09T07:49:35.910 回答
2
您可以简单地在 JQuery 中触发“popState”事件,例如:
$(window).on('popstate', function(event) {
alert("pop");
});
这足以满足您的要求......但是这里有一些添加窗口事件......</p>
$(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..
于 2018-02-19T00:16:32.933 回答
0
您可以使用popstate
来检测何时返回。
$(window).on('popstate', function(event) {
alert("You are going to back page. Can work that?");
});
button {
padding: 5px;
border-radius: 5px;
background: limegreen;
border: 1px limegreen solid;
color: white;
width: 100px;
}
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<button onclick="window.history.back()">Back</button>
<p>Or you can click on browser back button.</p>
</body>
</html>
谢谢你,如果工作.!.?
于 2021-05-25T18:59:53.107 回答