简体   繁体   English

关闭浏览器时确认对话框?

[英]Confirm Dialog when i close the browser?

i need to display confirm dialog box before close browser window using javascript or PHP. 在使用javascript或PHP关闭浏览器窗口之前,我需要显示确认对话框。 the confirm box should come when i click the close button of browser. 当我单击浏览器的关闭按钮时,确认框就会出现。 other wise don't display dialog. 否则不显示对话框。 please help me any. 请帮助我。

You should handle the onbeforeunload event... 您应该处理onbeforeunload事件...

function closeEditorWarning(){
    return 'Are you sure?'
}
window.onbeforeunload = closeEditorWarning;

Or use jquery, window.attachEvent / window.addEventListener to do it nicely 或者使用jquery,window.attachEvent / window.addEventListener很好地做到这一点

onunload is not very useful (in my opinion) as you can't do anything with the confirm ation you're requesting (except maybe attempt to new another window with window.open , so onbeforeunload is more useful for this case. onunload并不是很有用(在我看来),因为您无法所请求的confirm 任何事情(除了可能尝试使用window.open新建另一个窗口,因此onbeforeunload在这种情况下更有用。

Your better bet is onbeforeunload , which is great but won't work in Opera (though this usually isn't a deal breaker). 更好的选择是onbeforeunload ,这很好,但在Opera中不起作用(尽管这通常不会破坏交易)。

Like ivy said, it would look something like this: 就像常春藤说的那样,它看起来像这样:

<script>

    var userIsEditingSomething; // set this if something crazy happens
        oldOnBeforeUnload = window.onbeforeunload;

    window.onbeforeunload = function () {
        // attempt to handle a previous onbeforeunload
        if ('function' === typeof oldOnBeforeUnload) {
            var message = oldOnBeforeUnload();
            if ('undefined' !== typeof message) {
                if (confirm('string' === typeof message ? message : 'Are you sure you want to leave this page?')) {
                    return; // allow user to exit without further annoying pop-ups
                }
            }
        }
        // handle our own
        if (userIsEditingSomething) {
            return 'Are you sure you want to exit?';
        }
    };

</script>
function doUnload()
{
  // use confirm dialog box here
   confirm("Window is closing...");

}

<body onunload="doUnload()">

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

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