jQuery 页面刷新时如何防止调用onbeforeunload

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

How to prevent calling onbeforeunload when page refresh

jquery

提问by mymotherland

I want to prevent calling the function confirmExit()when the page is refreshed. Is it possible to do that?

我想防止confirmExit()在页面刷新时调用该函数。有可能这样做吗?

<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
   $.post("test.php");
}
</script>

回答by Darin Dimitrov

You can't. A page refresh is like navigating away and unloading the DOM so the onbeforeunloadevent will always be called.

你不能。页面刷新就像导航离开并卸载 DOM,因此onbeforeunload事件将始终被调用。

回答by edolopez

For what you are looking for, the best way to have control refreshing the webpage would be with the onKeyDown function. Unfortunately pressing the refresh button directly from your browser will load the DOM again, so technically there's no way to prevent the refresh from this action.

对于您正在寻找的内容,控制刷新网页的最佳方法是使用 onKeyDown 函数。不幸的是,直接从浏览器按下刷新按钮将再次加载 DOM,因此从技术上讲,无法阻止此操作的刷新。

Getting into the same issue, I found this webpage http://asquare.net/javascript/tests/KeyCode.htmlwhere you can verify the response of your browser to keyboard events. Here you can start figuring out why is onKeyDown the best option. Chrome doesn't react with the onKeyPress function.

遇到同样的问题,我找到了这个网页http://asquare.net/javascript/tests/KeyCode.html,您可以在其中验证浏览器对键盘事件的响应。在这里,您可以开始弄清楚为什么 onKeyDown 是最佳选择。Chrome 不会对 onKeyPress 函数做出反应。

You just need a variable to control the refreshing action. If the user presses the key, then the onBeforeUnload action won't be executed.

您只需要一个变量来控制刷新操作。如果用户按下该键,则不会执行 onBeforeUnload 操作。

var refresh = false;  //Control variable to control refresh access
j$(window).bind('beforeunload', function(){
    if (refresh == false) { // If F5 is not pressed
        return "Do you really want to leave?";
    }
});

j$(window).keydown(function(event) {
  if (event.keyCode == 116) { // User presses F5 to refresh
     refresh = true;
   }
});

回答by David Botsko

There are two things to consider. (1) The "window.onbeforeunload" function executes when the f5 key is released (onkeyup). Anything to modify the dialogue appearance must occur before that as onkeydown. (2) The function will not work if return is followed by a null value.

有两件事需要考虑。(1) “window.onbeforeunload”函数在f5键被释放(onkeyup)时执行。任何修改对话外观的操作都必须在 onkeydown 之前发生。(2) 如果 return 后跟空值,则该函数将不起作用。

If you set a global variable to use as a return value AND make it null if the f5 key is pressed, the dialog does not appear when f5 is used to refresh. It does however appear if the "Close" button is pressed. Note that the code inside "onbeforeunload" will run even if the return value is null so you must check for your return value if you want the code to be disabled also.

如果将全局变量设置为用作返回值并在按下 f5 键时将其设为 null,则在使用 f5 刷新时不会出现该对话框。但是,如果按下“关闭”按钮,它就会出现。请注意,即使返回值为空,“onbeforeunload”中的代码也会运行,因此如果您还希望禁用代码,则必须检查返回值。

document.onkeydown = KeyCheck;
window.returnMessage="You are now logged out.";
function KeyCheck(e) {
    var key = (window.event) ? event.keyCode : e.keyCode;
    alert(key);
    if(key==116) {window.returnMessage=null;}
    }
$(function(){
    window.onbeforeunload = function(event) {
        if(window.returnMessage.length > 0) {logoutFunction();}
        return window.returnMessage;
        }   
    });