简体   繁体   English

如何阻止用户在 Javascript 中关闭窗口?

[英]How to block users from closing a window in Javascript?

Is it possible to block users from closing the window using the exit button [X] ?是否可以使用退出按钮[X]阻止用户关闭窗口? I am actually providing a close button in the page for the users to close the window.Basically what I'm trying to do is to force the users to fill the form and submit it.我实际上在页面中为用户提供了一个关闭按钮来关闭窗口。基本上我要做的是强制用户填写表单并提交它。 I don't want them to close the window till they have submitted it.我不希望他们在提交之前关闭窗口。

I really appreciate your comments, I'm 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...我知道这不是正确的方法,但我想知道我们遇到的问题是否有解决方案......

Take a look at onBeforeUnload .看看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 report from the 18th of February, 2016.请参阅 2016 年 2 月 18 日的此错误报告

onbeforeunload dialogs are used for two things on the Modern Web: onbeforeunload 对话框用于现代 Web 上的两件事:
1. Preventing users from inadvertently losing data. 1、防止用户不慎丢失数据。
2. Scamming users. 2. 欺骗用户。

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[...] Firefox 已经这样做了[...]

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这对我来说很好用

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 两次,它将关闭孩子和父母(如果它是一个弹出窗口)

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 extremely annoying.请记住,这非常烦人。 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.如果您试图强迫您的用户填写他们不想填写的表单,那么您将失败:他们会找到关闭窗口的方法,并且永远不会回到您的卑鄙网站。

How about that?那个怎么样?

function internalHandler(e) {
    e.preventDefault(); // required in some browsers
    e.returnValue = ""; // required in some browsers
    return "Custom message to show to the user"; // only works in old browsers
}

if (window.addEventListener) {
    window.addEventListener('beforeunload', internalHandler, true);
} else if (window.attachEvent) {
    window.attachEvent('onbeforeunload', internalHandler);
}

如果您发送的内部调查需要贵公司员工 100% 参与,那么更好的方法是让表单跟踪响应者 ID/用户名/电子邮件等。每隔几天左右就发送一个不错的小通过电子邮件提醒您组织中的人员完成调查……您甚至可以自动执行此操作。

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 div on your current web page to layer over top the rest of your controls so your form is the only thing accessible.但是,您可以通过在当前网页上创建一个div来覆盖其余控件的顶部,这样您的表单是唯一可访问的内容,从而实现类似的效果。

Well you can use the window.onclose event and return false in 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.另一方面,如果他们强制关闭(通过使用任务管理器或这些行中的某些内容),您将无能为力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM