如何阻止用户在 javascript 中关闭窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2229942/
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
how to block users from closing a window in javascript?
提问by manraj82
Is it possible to block users from closing the window using the exit button [X]? I am actually providing a close button in the page for the users to close the window.Basically what im trying to do is to force the users to fill the form and submit it.I dont want them to close the window till they have submitted it.
是否可以使用退出按钮 [X] 阻止用户关闭窗口?我实际上在页面中提供了一个关闭按钮供用户关闭窗口。基本上我试图做的是强制用户填写表单并提交它。我不希望他们关闭窗口直到他们提交它.
guys i really appreciate your comments,im not thinking of hosting on any commercial website.its an internal thing,we are actually getting all the staff to participate in this survey we have designed.... i know its not the right way but i was wondering if there was a solution to the problem we have got here...
伙计们,我非常感谢您的评论,我不想在任何商业网站上托管。这是内部事务,我们实际上正在让所有员工参与我们设计的这项调查......我知道这不是正确的方式,但我想知道我们遇到的问题是否有解决方案...
回答by Pool
Take a look at onBeforeUnload.
It wont force someone to stay but it will prompt them asking them whether they really want to leave, which is probably the best cross browser solution you can manage. (Similar to this site if you attempt to leave mid-answer.)
它不会强迫某人留下,但会提示他们询问他们是否真的想离开,这可能是您可以管理的最佳跨浏览器解决方案。(如果您尝试离开中间答案,则类似于此站点。)
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit() {
return "You have attempted to leave this page. Are you sure?";
}
</script>
Edit: Most browsers no longer allow a custom message for onbeforeunload.
编辑:大多数浏览器不再允许自定义消息onbeforeunload。
See this bug reportfrom the 18th of February, 2016.
请参阅2016 年 2 月 18 日的此错误报告。
onbeforeunload dialogs are used for two things on the Modern Web:
1. Preventing users from inadvertently losing data.
2. Scamming users.In an attempt to restrict their use for the latter while not stopping the former, we are going to not display the string provided by the webpage. Instead, we are going to use a generic string.
Firefox already does this[...]
onbeforeunload 对话框在现代 Web 上用于两件事:
1. 防止用户无意中丢失数据。
2. 欺骗用户。为了在不阻止前者的同时限制它们对后者的使用,我们将不显示网页提供的字符串。相反,我们将使用通用字符串。
Firefox 已经这样做了[...]
回答by SQLMenace
What will you do when a user hits ALT + F4 or closes it from Task Manager
当用户按下 ALT + F4 或从任务管理器关闭它时你会做什么
Why don't you keep track if they did not complete it in a cookie or the DB and when they visit next time just bring the same screen back...:BTW..you haven't finished filling this form out..."
如果他们没有在 cookie 或数据库中完成它,你为什么不跟踪它,当他们下次访问时只需将相同的屏幕带回来......:顺便说一句......你还没有完成填写这个表格...... ”
Of course if you were around before the dotcom bust you would remember porn storms, where if you closed 1 window 15 others would open..so yes there is code that will detect a window closing but if you hit ALT + F4 twice it will close the child and the parent (if it was a popup)
当然,如果您在互联网泡沫破灭之前就在附近,您会记得风暴,如果您关闭 1 个窗口,其他 15 个窗口将打开..所以是的,有代码可以检测窗口关闭,但如果您按 ALT + F4 两次,它将关闭孩子和父母(如果它是一个弹出窗口)
回答by Imam
If you don't want to display popup for all event you can add conditions like
如果您不想为所有事件显示弹出窗口,您可以添加条件,例如
window.onbeforeunload = confirmExit;
function confirmExit() {
if (isAnyTaskInProgress) {
return "Some task is in progress. Are you sure, you want to close?";
}
}
This works fine for me
这对我来说很好用
回答by Gipsy King
This will pop a dialog asking the user if he really wants to close or stay, with a message.
这将弹出一个对话框,询问用户是否真的想关闭或留下,并带有一条消息。
var message = "You have not filled out the form.";
window.onbeforeunload = function(event) {
var e = e || window.event;
if (e) {
e.returnValue = message;
}
return message;
};
You can then unset it before the form gets submitted or something else with
然后,您可以在提交表单或其他内容之前取消设置它
window.onbeforeunload = null;
Keep in mind that this is extremelyannoying. If you are trying to force your users to fill out a form that they don't want to fill out, then you will fail: they will find a way to close the window and never come back to your mean website.
请记住,这非常烦人。如果您试图强迫您的用户填写他们不想填写的表单,那么您将失败:他们会找到关闭窗口的方法,并且永远不会回到您的卑鄙网站。
回答by John Lechowicz
If your sending out an internal survey that requires 100% participation from your company's employees, then a better route would be to just have the form keep track of the responders ID/Username/email etc. Every few days or so just send a nice little email reminder to those in your organization to complete the survey...you could probably even automate this.
如果您发送一份需要公司员工 100% 参与的内部调查,那么更好的方法是让表单记录响应者 ID/用户名/电子邮件等。每隔几天左右就发送一个不错的小通过电子邮件提醒您组织中的人员完成调查……您甚至可以自动执行此操作。
回答by Jonathon Faust
It's poor practice to force the user to do something they don't necessarily want to do. You can't ever really prevent them from closing the browser.
强迫用户做他们不一定想做的事情是一种糟糕的做法。您永远无法真正阻止他们关闭浏览器。
You can achieve a similar effect, though, by making a divon your current web page to layer over top the rest of your controls so your form is the only thing accessible.
但是,您可以通过div在当前网页上创建一个以覆盖其余控件的顶部来实现类似的效果,因此您的表单是唯一可访问的内容。
回答by alun
How about that?
那个怎么样?
function internalHandler(e) {
return "Please don't leave our page ever!";
}
if (window.addEventListener) {
window.addEventListener('beforeunload', internalHandler, true);
} else if (window.attachEvent) {
window.attachEvent('onbeforeunload', internalHandler);
}
回答by jpabluz
Well you can use the window.oncloseevent and return falsein the event handler.
那么您可以使用该window.onclose事件并false在事件处理程序中返回。
function closedWin() {
confirm("close ?");
return false; /* which will not allow to close the window */
}
if(window.addEventListener) {
window.addEventListener("close", closedWin, false);
}
window.onclose = closedWin;
Code was taken from this site.
代码取自该站点。
In the other hand, if they force the closing (by using task manager or something in those lines) you cannot do anything about it.
另一方面,如果他们强制关闭(通过使用任务管理器或这些行中的某些内容),您将无能为力。

