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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18457797/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 12:00:43  来源:igfitidea点击:

How to know whether refresh button or browser back button is clicked in Firefox

javascriptbrowsercross-browserdom-events

提问by Coding

How to know in Firefox whether refresh button is clicked or browser back button is clicked... for both events onbeforeunload()method is a callback. For IE I am handling like this:

如何在 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();"> 

But in Firefox event.clientXand event.clientYare always 0. Is there any other way to find it?

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

回答by Er.KT

Use for on refresh event

用于刷新事件

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

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

And

$(window).unload(function() {
      alert('Handler for .unload() called.');
});

回答by Vipin Malik

Use 'event.currentTarget.performance.navigation.type' to determine the type of navigation. This is working in IE, FF and Chrome.

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

回答by developerCK

For Back Button in jquery // http://code.jquery.com/jquery-latest.js

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

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

and in html5 there is an event The event is called 'popstate'

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

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

and for refresh please check Check if page gets reloaded or refreshed in Javascript

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

In Mozilla Client-x and client-y is inside document area https://developer.mozilla.org/en-US/docs/Web/API/event.clientX

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

回答by mostafa khansa

var keyCode = evt.keyCode;
if (keyCode==8)
alert('you pressed backspace');

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