【问题标题】:How to know whether refresh button or browser back button is clicked in Firefox [duplicate]如何知道Firefox中是否单击了刷新按钮或浏览器后退按钮[重复]
【发布时间】:2013-08-29 17:59:33
【问题描述】:

如何在 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。有没有其他方法可以找到它?

【问题讨论】:

  • 您可以使用历史 API 并自己处理整个导航过程。见developer.mozilla.org/en-US/docs/Web/Guide/DOM/…
  • 注意刷新也可以用F5、CTRL+R,导航也可以用键盘,不同浏览器和操作系统不同。
  • 顺便说一句,你想跨浏览器还是只在 Firefox 中?你的标题是 firefox,但标签是跨浏览器。
  • 只检查缓存中是否有值。如果有一个后退按钮,那么一些保存的值仍然存在,如果单击刷新按钮,那么一切都将是默认值。这在 PHP 中很容易完成,但如果您只检查设置变量,也可以在 Javascript 中完成。
  • 经验法则:每当您觉得“需要”区分刷新或后退/前进导航时,您很可能已经做错了什么。所以我建议你描述一下你想通过这个解决的实际问题,而不是继续骑这匹已经死(或者更确切地说已经死胎)的马。

标签: javascript browser cross-browser dom-events


【解决方案1】:

返回按钮 在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));
};

并刷新请检查 Check if page gets reloaded or refreshed in Javascript

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

【讨论】:

  • 同样的东西在 html4 上也有效
  • popstate 事件在 html5 中定义。所以它只适用于兼容 html5 的浏览器。
【解决方案2】:

用于刷新事件

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.');
});

【讨论】:

  • 这如何区分重新加载和返回按钮?
  • 它没有。另请注意,iOS 故意不支持beforeunload 事件。
  • jQuery.unload() 已被删除。请参阅stackoverflow.com/questions/23445332/…
  • 不回答问题:-\
  • 它可以,但我们可以更改文本
【解决方案3】:

使用“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");
        }           
    }
}

【讨论】:

【解决方案4】:
var keyCode = evt.keyCode;
if (keyCode==8)
alert('you pressed backspace');

if(keyCode==116)
alert('you pressed f5 to reload page')

【讨论】:

  • 如果某些用户使用鼠标而不是按键怎么办?
  • 我没有给出完整的解决方案,我只是提出一个想法来帮助,最终程序员应该努力解决
  • 还有鼠标导航,Mozilla开发者网站上有完整的教程,例如window.back,它是一个javascript函数,用于指向上一页的浏览器
  • 不同的浏览器/操作系统刷新浏览器的方式不同
  • 为什么这个世界被低估了!!!???它不是是一个糟糕的解决方案,因为它可以让你大部分时间到达那里,即使键码在某些平台之间有所不同。 这实际上比beforeunload 更接近解决方案,因为beforeunload 将触发任何时候页面被卸载并且不一定刷新——可能永远。这至少针对刷新,单独。但我很想反对使用== 而不是=== ;-P
猜你喜欢
  • 2019-03-04
  • 1970-01-01
  • 2010-09-22
  • 2013-06-23
  • 1970-01-01
  • 2013-03-19
  • 2011-09-15
  • 1970-01-01
  • 2011-09-06
相关资源
最近更新 更多