简体   繁体   English

window.onbeforeunload上的confirm()

[英]confirm() on window.onbeforeunload

I want to show a confirmation dialog if the user wants to leave the page with unsaved form data. 如果用户想要使用未保存的表单数据离开页面,我想显示一个确认对话框。 What I have is: 我有的是:

window.onbeforeunload = function() {
    if (not_saved) if (confirm('Changes will not be saved')) return true;
    return false;
}

But no matter what the user clicks, the result is the same - the stupid hardcoded dialog box that asks them whether they want to leave the page or stay on page. 但无论用户点击什么,结果都是相同的 - 愚蠢的硬编码对话框询问他们是否要离开页面或留在页面上。 What I want to ask them is whether they want to stay or leave, and if they want to stay nothing happens, if they want to leave, they leave. 我想问他们的是他们是想留下还是离开,如果他们想留下什么,如果他们想要离开,他们就会离开。 Is this even possible? 这甚至可能吗? I can see how browsers want to limit what websites can do about keeping the users on the page, but I think I've seen some websites (Google Docs I think) having nice civilized dialog boxes. 我可以看到浏览器如何限制网站可以做些什么来保持用户在页面上,但我想我已经看到一些网站(我认为Google Docs)有很好的文明对话框。

Although window.onbeforeunload works, it's considered bad practice. 尽管window.onbeforeunload有效,但它被认为是不好的做法。 It's better to use something like the following: 最好使用以下内容:

if (typeof window.addEventListener === 'undefined') {
    window.addEventListener = function(e, callback) {
        return window.attachEvent('on' + e, callback);
    }
}

window.addEventListener('beforeunload', function() {
    return 'Dialog Text Here';
});

First we check if window.addEventListener exists, which it does not in IE, else we polyfill it, and then we attach the event. 首先我们检查window.addEventListener存在,它在IE中不存在,否则我们将其填充,然后我们附加事件。

window.onbeforeunload = function(e) {
    return 'Dialog text here.';
};

Docs: 文档:

Note that in Firefox 4 and later the returned string is not displayed to the user. 请注意,在Firefox 4及更高版本中,返回的字符串不会显示给用户。

If the returnValue attribute of the event object is not the empty string, or if the event was canceled, then the user agent should ask the user to confirm that they wish to unload the document. 如果事件对象的returnValue属性不是空字符串,或者事件已取消,则用户代理应该要求用户确认他们是否要卸载该文档。 The prompt shown by the user agent may include the string of the returnValue attribute, or some leading subset thereof. 用户代理所示的提示可以包括returnValue属性的字符串,或其某些前导子集。 (A user agent may want to truncate the string to 1024 characters for display, for instance.) (例如,用户代理可能希望将字符串截断为1024个字符以供显示。)

With jQuery, you can set the same handlers. 使用jQuery,您可以设置相同的处理程序。 The code may defer based on your version. 代码可能会根据您的版本推迟。

    function AnyFunction(e){ /**** your code *****/ };

    $(function () {
      $(window).bind('unload', function (e) { return AnyFunction(e); });
      $(window).bind('beforeunload', function (e) { return AnyFunction(e); });
    });

You can also change the confirm or alert function using plugins as bootbox . 您还可以使用插件作为bootbox更改确认或警报功能

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

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