如何知道在 Firefox 中是否单击了刷新按钮或浏览器后退按钮

IT技术 javascript browser cross-browser dom-events
2021-01-19 07:37:04

如何在 Firefox 中知道是点击了刷新按钮还是点击了浏览器后退按钮...对于这两个事件onbeforeunload()方法都是回调。对于 IE,我是这样处理的:

function CallbackFunction(event) {
    if (window.event) {
        if (window.event.clientX < 40 && window.event.clientY < 0) {
            alert("back button is clicked");
        }else{
            alert("refresh button is clicked");
        }
    }else{
        // want some condition here so that I can differentiate between
        // whether refresh button is clicked or back button is clicked.
    }
}

<body onbeforeunload="CallbackFunction();"> 

但是在 Firefox 中event.clientXevent.clientY 始终为 0。有没有其他方法可以找到它?

4个回答

用于刷新事件

window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

https://developer.mozilla.org/en-US/docs/Web/API/window.onbeforeunload?redirectlocale=en-US&redirectslug=DOM%2Fwindow.onbeforeunload

$(window).unload(function() {
      alert('Handler for .unload() called.');
});
不回答问题:-\
2021-03-17 07:37:04
jQuery.unload() 已被删除。请参阅stackoverflow.com/questions/23445332/...代替
2021-03-25 07:37:04
它有效,但我们可以更改文本
2021-03-30 07:37:04
它没有。另请注意,iOS 故意不支持beforeunload事件。
2021-04-02 07:37:04
这如何区分重新加载和后退按钮?
2021-04-08 07:37:04

使用“event.currentTarget.performance.navigation.type”来确定导航类型。这适用于 IE、FF 和 Chrome。

function CallbackFunction(event) {
    if(window.event) {
        if (window.event.clientX < 40 && window.event.clientY < 0) {
            alert("back button is clicked");
        }else{
            alert("refresh button is clicked");
        }
    }else{
        if (event.currentTarget.performance.navigation.type == 2) {
            alert("back button is clicked");
        }
        if (event.currentTarget.performance.navigation.type == 1) {
            alert("refresh button is clicked");
        }           
    }
}
如何使用这个功能?
2021-03-16 07:37:04
event.currentTarget.performance.navigation.type 对我不起作用,甚至 event.currentTarget.performance 对我来说都显示为未定义。你会在这方面提供更多帮助吗,更多的代码或脚本。
2021-03-28 07:37:04
它说明页面是如何导航到的,而不是现在发生了什么。stackoverflow.com/questions/35468281/...
2021-03-31 07:37:04
2021-04-05 07:37:04
你能提供你正在使用的FF版本吗?
2021-04-13 07:37:04

对于 jquery 中的后退按钮 // http://code.jquery.com/jquery-latest.js

 jQuery(window).bind("unload", function() { //

并且在 html5 中有一个事件 该事件称为“popstate”

window.onpopstate = function(event) {
alert("location: " + document.location + ", state: " + JSON.stringify(event.state));
};

对于刷新,请检查 检查页面是否在 Javascript 中重新加载或刷新

在 Mozilla Client-x 和 client-y 位于文档区域内 https://developer.mozilla.org/en-US/docs/Web/API/event.clientX

同样的事情适用于 html4
2021-03-17 07:37:04
popstate 事件在 html5 中定义。所以它只适用于 html5 兼容的浏览器。
2021-04-05 07:37:04
var keyCode = evt.keyCode;
if (keyCode==8)
alert('you pressed backspace');

if(keyCode==116)
alert('you pressed f5 to reload page')
为什么这个世界被低估了!!!???不是一个糟糕的解决方案,因为它可以让您完成大部分工作,即使某些平台之间的键码有所不同。这实际上是更接近的解决方案比beforeunloadbeforeunload是要火随时页面就会被卸载,没有必要在刷新-可能永远。这至少针对刷新,单独。但我很想为 using==而不是===;-P 投反对票
2021-03-17 07:37:04
我没有给出完整的解决方案,我只是提出了一个帮助的想法,最终程序员应该努力解决
2021-03-18 07:37:04
以及通过鼠标导航,Mozilla 开发人员网站上有完整的教程,例如 window.back,它是一个 javascript 函数,用于指向上一页的浏览器
2021-03-21 07:37:04
如果某些用户使用鼠标而不是按键怎么办?
2021-03-22 07:37:04
不同的浏览器/操作系统有不同的方式来刷新浏览器
2021-04-14 07:37:04