如何阻止用户在javascript中关闭窗口? [英] how to block users from closing a window in javascript?

查看:312
本文介绍了如何阻止用户在javascript中关闭窗口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用退出按钮[X]阻止用户关闭窗口?我实际上在页面中提供了一个关闭按钮,供用户关闭窗口。基本上我试图做的是强制用户填写表单并提交它。我不希望他们关闭窗口,直到他们提交它。

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.

伙计们,我非常感谢您的评论,我不想在任何商业网站上托管。内部事情,我们实际上让所有员工参与此调查我们设计....
i知道它不是正确的方法,但我想知道我们在这里遇到的问题是否有解决方案...

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...

推荐答案

查看 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>

编辑:大多数浏览器不再允许的自定义消息onbeforeunload

Most browsers no longer allow a custom message for onbeforeunload.

请参阅错误报告,2016年2月18日。

See this bug report from the 18th of February, 2016.


onbeforeunload对话框在现代网站上用于两件事:

1.防止用户无意中丢失数据。

2.欺骗用户。

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已经执行此操作[...]

Firefox already does this[...]

这篇关于如何阻止用户在javascript中关闭窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆