有人知道如何使用jquery或javascript在Firefox中检测浏览器刷新和后退按钮事件吗?
有人知道如何使用jquery或javascript在Firefox中检测浏览器刷新和后退按钮事件吗?
返回按钮:
window.addEventListener('popstate', function (event) {
//Your code here
});
刷新:
window.onbeforeunload = function () {
// Your code here
}
window.onbeforeunload = function(e) {
};
并且
$(window).unload(function() {
//
});
还要检查浏览器返回按钮检测:
I have made a very reusable javascript class, that can be simply dropped into your web page, and when the user clicks back, it will call a function. The default function on this call is a javascript alert “Back Button Clicked”.
To replace this functionality, you simply need to override the OnBack function. This can be done by using the code below.
<script type="text/javascript"> bajb_backdetect.OnBack = function() { alert('You clicked it!'); } </script>
This will now replace the “Back Button Clicked” alert with a “You clicked it!’” alert.
window.onpopstate
。window.onpopstate = (e) => {
// your logic goes here
};
window.onbeforeunload
用于刷新或关闭选项卡。
window.onbeforeunload = (e) => {
// your logic here
e.preventDefault();
e.returnValue = 'There are unsaved changes. Sure you want to leave?';
};