javascript 检测浏览器关闭事件

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

Detect browser close event

javascriptbrowsercross-browseronbeforeunload

提问by Rohit Jindal

I want to capture the browser window/tab close event. I have tried the following But not working:

我想捕获浏览器窗口/选项卡关闭事件。我尝试了以下但不起作用:

<script type="text/javascript">
window.onbeforeunload = function(){ myUnloadEvent(); }
function myUnloadEvent() {
    alert ('Calling some alert messages here');
}
</script>
<body>
Body of the page goes here.
</body>

Tried these links also but no more success.

也尝试了这些链接,但没有成功。

javascript to check when the browser window is close
How to capture the browser window close event?
javascript detect browser close tab/close browser
Trying to detect browser close event

javascript 检查浏览器窗口何时关闭
如何捕获浏览器窗口关闭事件?
javascript检测浏览器关闭标签/关闭浏览器
尝试检测浏览器关闭事件

Problem :

问题 :

I just want to detect the browser close event and do some handling on that without showing the prompt to the user.

我只想检测浏览器关闭事件并对其进行一些处理而不向用户显示提示。

I have tried many methods to detect browser close event through jQuery or JavaScript. But unfortunately I could not succeed. The onbeforeunload and onunload methods are also not working.

我尝试了很多方法来通过 jQuery 或 JavaScript 检测浏览器关闭事件。但不幸的是我没能成功。onbeforeunload 和 onunload 方法也不起作用。

Any help will be highly appreciated. Thanks

任何帮助将不胜感激。谢谢

采纳答案by Zee

You cannot show an alert()on an onbeforeunload. You need to returnto show a confirm dialog.

您不能在alert()上显示onbeforeunload。您需要return显示一个confirm dialog.

<script type="text/javascript">
window.onbeforeunload = function(e){  
  return 'Calling some alert messages here'; //return not alert
}
</script>
<body>
Body of the page goes here.
</body>

Also do not call a functionlike that, write directly in the onbeforeunloadOR return myUnloadEvent();It depends what you are trying to do inside onbeforeunload. Try not to make any AJAX call as the browser may not run the script. You can unset web storage varibles, delete session, etc.

也不要那样叫a function,直接写在onbeforeunloadORreturn myUnloadEvent();里面就看你想做什么了onbeforeunload。尽量不要进行任何 AJAX 调用,因为浏览器可能不会运行该脚本。您可以取消设置 Web 存储变量、删除会话等。

Also beware that this event will also fire when you refresh the page.

另请注意,刷新页面时也会触发此事件。

回答by philz

You can see the console quickly write that line before the screen closes.

您可以看到控制台在屏幕关闭之前快速写入该行。

window.onbeforeunload = myUnloadEvent;
function myUnloadEvent() {
    console.log("Do your actions in here")
}

Alerts are not allowed during beforeunload - if you have Preserve log switched on you will see this:

在卸载之前不允许发出警报 - 如果您打开了保留日志,您将看到以下内容:

Blocked alert('Calling some alert messages here') during beforeunload.

But if you use a console.log command, it will go through.

但是如果你使用 console.log 命令,它会通过。