简体   繁体   English

与onbeforeunload一起出现问题并进行确认

[英]trouble with onbeforeunload in conjunction with confirm

I've written some code that's supposed to fire a confirm dialog box when a user leaves the page. 我编写了一些代码,这些代码应该在用户离开页面时触发确认对话框。 If the user provides confirmation, a popup window is supposed to appear; 如果用户提供确认,则将出现一个弹出窗口; otherwise, it shouldn't. 否则,不应该。 However, the onbeforeunload bit isn't firing—I don't understand why. 但是,onbeforeunload位没有触发-我不明白为什么。 As it's probably apparent, I'm kind of a JavaScript newbie. 显而易见,我是JavaScript新手。

window.onbeforeunload = function () {
    if (confirm("Would you like to take a short survey?")) {
        w = 1000;
        h = 1000;
        var left = (screen.width/2)-(w/2);
        var top = (screen.height/2)-(h/2);
        window.open('http://www.google.com','toolbar=0,resizable = 1, scrollbars = 1, width='+w+',height='+h+', top ='+top+', left='+left);
    }
}

Since 25 May 2011, the HTML5 specification states that calls to window.alert(), window.confirm(), and window.prompt() methods may be ignored during this event. 从2011年5月25日开始,HTML5规范规定在此事件期间可以忽略对window.alert(),window.confirm()和window.prompt()方法的调用。 See the HTML5 specification for more details. 有关更多详细信息,请参见HTML5规范。

Source: developer.mozilla.org 资料来源: developer.mozilla.org

The function should return the text to be displayed in the confirm dialog: 该函数应返回要在确认对话框中显示的文本:

window.onbeforeunload = function(e) {
    return 'You are leaving this page.';
};

but it will not necessarily work in all browsers. 但不一定在所有浏览器中都可以。

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

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