使用 javascript(或任何其他语言)的浏览器/标签关闭检测

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9941354/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:18:14  来源:igfitidea点击:

Browser/tab close detection using javascript (or any other language)

javascriptjavascript-events

提问by gopi1410

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.

我在各个地方搜索过这个问题,但他们提到的只是使用 javascript window.unload& window.onbeforeunload。此外,它在大多数情况下无法在 chrome 中工作,因为它被阻止了。
那么谷歌如何设法做到这一点。如果我们正在撰写邮件并错误地关闭了选项卡,谷歌会通过“你确定吗?”来提示我们。盒子。
有人可以帮我吗?
我实际上想要做的是要求用户确认,当他填写表格并错误地点击选项卡关闭时。如果是,我允许他离开,否则他会留在页面上并继续填写表格。

回答by Peter Aron Zentai

I tested and the example posted on MDN works clearly in Chrome.

我测试过,MDN 上发布的示例在 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.

这样,弹出窗口阻止程序不会阻止的对话框打开,因为它源自用户单击。

回答by Joel Purra

If you only want to ask a user if they want to leave the page when they've changed anything in the forms on the page, have a look at PageHasFormChanges. Keep in mind that I developed it for use it on very simple pages, with only one or two forms.

如果您只想询问用户在更改页面上的表单中的任何内容后是否要离开页面,请查看PageHasFormChanges。请记住,我开发它是为了在非常简单的页面上使用它,只有一两个表单。

PageHasFormChanges: A jQuery plugin to check if anything has changed in any form on a page, and warn the user before leaving the page.

PageHasFormChanges:一个 jQuery 插件,用于检查页面上任何形式的任何内容是否发生了变化,并在离开页面之前警告用户。

You don't need to do anything, but load the script on your page. You can change the defaults though:

您无需执行任何操作,只需在页面上加载脚本即可。您可以更改默认值:

JoelPurra.PageHasFormChanges.setOptions({
    leavingPageWarningMessage: "Are you sure?"
});

If you're using ajax to submit your forms, you could consider setting resetWarningOnPreventedSubmitto true.

如果您使用 ajax 提交表单,则可以考虑设置resetWarningOnPreventedSubmittrue.

回答by Sujit Agarwal

you can use the jquery unloadfunction:

您可以使用jquery 卸载功能:

$(window).unload(function() {
  alert('Handler for .unload() called.');
});