Javascript 在浏览器关闭之前提示用户?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2923139/
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
Prompt User before browser close?
提问by JM4
We have an administrative portal that our teachers constantly forget to download their latest PDF instructions before logging out and/or closing the browser window. I have looked around but can't find what I'm looking for.
我们有一个管理门户,我们的教师经常忘记在注销和/或关闭浏览器窗口之前下载他们最新的 PDF 说明。我环顾四周,但找不到我要找的东西。
I want to accomplish the following goals:
我想实现以下目标:
Goal 1
目标 1
Before a user can close the browser window, they are prompted "Did you remember to download your form?" with two options, yes/no. If yes, close, if no, return to page.
在用户关闭浏览器窗口之前,系统会提示他们“您记得下载表单吗?” 有两个选项,是/否。如果是,关闭,如果不是,返回页面。
Goal 2
目标 2
Before a user can click the 'logout' button, they are prompted with the same as above.
在用户单击“注销”按钮之前,他们会收到与上述相同的提示。
My first pass at the very basic code (which does not work for browser close) is:
我对非常基本的代码(不适用于浏览器关闭)的第一遍是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
function init() {
if (window.addEventListener) {
window.addEventListener("beforeunload", unloadMess, false);
} else if (window.onbeforeunload) {
window.onbeforeunload = unloadMess;
};
}
function unloadMess() {
var User_Message = "[Your user message here]"
return User_Message;
}
</script>
</head>
<body onload="init();">
hello this is my site
</body>
</html>
EDIT - NEW ISSUES
编辑 - 新问题
The solution provided below works but also has its own issues:
下面提供的解决方案有效,但也有其自身的问题:
When user clicks the link to actually download the forms, the page thinks they are trying to leave and in turn present an error message to them! Also - I would like for one event or another to occur but not necessarily both. I.e. they hit 'logout' button and they are presented with the OnClick event, THEN the browser prompt. Any ideas?
当用户单击链接以实际下载表单时,该页面认为他们正试图离开并反过来向他们显示错误消息!另外 - 我希望一个事件或另一个事件发生,但不一定同时发生。即他们点击“注销”按钮,然后他们会看到 OnClick 事件,然后是浏览器提示。有任何想法吗?
回答by Mohit Jain
Update 2017
2017 年更新
All modern browsers do not support custom messages any longer.
所有现代浏览器不再支持自定义消息。
window.onbeforeunload = function(evt) {
return true;
}
This one for closing the tab:
这是用于关闭选项卡的:
window.onbeforeunload = function(evt) {
var message = 'Did you remember to download your form?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
and use onClickevent for logout button:
并使用onClick事件注销按钮:
onClick="return confirm('Did you remember to download your form?');"
回答by Pointy
I think that this may be part of your problem:
我认为这可能是您问题的一部分:
else if(window.onbeforeunload) {
window.onbeforeunload = unloadMess;
};
That test in the "if" statement will only be true if there's already a handler function. That test doesn't mean, "does the window object have an 'onbeforeunload' property?". It means, "is the 'onbeforeunload' property of the window currently not null?".
“if”语句中的测试只有在已经存在处理函数时才会为真。该测试并不意味着“窗口对象是否具有 'onbeforeunload' 属性?”。这意味着,“当前窗口的 'onbeforeunload' 属性是否为空?”。
I think it'd be safe to just set "onbeforeunload" directly, for any browser.
我认为直接为任何浏览器设置“onbeforeunload”是安全的。
回答by Marian
You cannot alert or things like that in onbeforeunload, you cannot simply return false to make the user not leave the page, as with other events like onclick. This would allow a site to make it impossible to leave it.
您不能在 中发出警报或类似的事情onbeforeunload,也不能像onclick. 这将使网站无法离开它。
You can however just return a string, the browser then shows a confirm dialog including your string asking the user whether they really want to leave.
但是,您可以只返回一个字符串,然后浏览器会显示一个确认对话框,其中包含您的字符串,询问用户是否真的想离开。
回答by Ryley
Pointy is right about browser close events - imagine if evil sites could prevent you from closing their windows?? So you cannot preventthem from closing your site, but you can do an alert.
Pointy 关于浏览器关闭事件是正确的 - 想象一下,如果邪恶的网站可以阻止你关闭他们的窗口?因此,您无法阻止他们关闭您的网站,但您可以发出警报。
As far as your logout button is concerned, that is much more straight-forward:
就您的注销按钮而言,这更加直接:
<a href="logout.html" onClick="return confirm('Did you remember to download your form?');">Logout</a>

