浏览器/选项卡使用javascript(或任何其他语言)关闭检测 [英] Browser/tab close detection using javascript (or any other language)
问题描述
我在不同的地方搜索过这个问题,但他们提到的只是使用javascript window.unload & window.onbeforeunload 。此外,它在大多数情况下都不能用于Chrome,因为它被阻止。
然后google如何设法做到这一点。如果我们正在撰写邮件并错误地关闭标签,谷歌会提示我们你确定吗? BO $ b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b b错误点击标签关闭。如果是的话,我允许他离开其他他留在页面&继续填写他的表格。
I searched for this question in various places, but all that they mention is the use of javascript window.unload & window.onbeforeunload. Also it doesn't work in chrome most of the times as it gets blocked.
Then how does google manage to do it. If we are composing a mail and by mistake close the tab, google prompts us by a "Are you sure?" box.
Can somebody help me out?
What actually I want to do is to ask confirmation of the user, when he is filling in the form and by mistake clicks on tab close. If yes, I allow him to navigate away else he stays on the page & continues to fill his form.
推荐答案
我测试过,在MDN上发布的示例在Chrome中清晰可见。
I tested and the example posted on MDN works clearly in Chrome.
<script>
window.onbeforeunload = function (e) {
e = e || window.event;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = 'Any string';
}
// For Safari
return 'Any string';
};
function formSubmit() {
window.onbeforeunload = null;
}
</script>
...
<form onsubmit="formSubmit()">...</form>
这样一个对话框打开,不会被弹出窗口阻止程序阻止,因为它来自用户点击。
This way a dialog opens that will not be blocked by a popup blocker as it is originated from a user click.
这篇关于浏览器/选项卡使用javascript(或任何其他语言)关闭检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!