简体   繁体   English

卸载前确认对话框

[英]Confirm dialog onbeforeunload

Hi I have read lots of other posts on the subject but am still confused. 嗨,我读过很多关于这个主题的其他文章,但仍然感到困惑。 I just want to achieve 2 simple things before my page is unloaded. 我只想在卸载页面之前实现2个简单的事情。

  1. If there is any unsaved data save it. 如果有任何未保存的数据,请保存它。 Ask yes/no to save it (using confirm()) 询问是/否保存(使用confirm())
  2. Save current selections on the page 在页面上保存当前选择

I am doing it as follows. 我正在做如下。

window.onbeforeunload = function (e) {


    var model = _selectedClassNodes + ";" + _selectedIndicatorNodes + ";" + _selectedNode + ";" + _pageHeaderId;

    $.ajax({
        url: "@Url.Action("SaveDefaultSettings", "Maps")",
        type: 'get',
        data: { defaultSettings: model },
        success: function (data) {


        }
    });

    //if some unsaved data exists
    if (editedRows.length > 0) {

        if (confirm("Do you wish to save changes before continuing? CLick 'OK' for 'Yes' and 'Cancel' for 'No'")) {
            SaveGridData();
        }
    }


};

The SaveDefaultSettings is always fired as i have debugged with a break point but no confirm box appears. 由于我已经调试了断点,因此总是会触发SaveDefaultSettings,但不会出现确认框。

As far as I remember confirm() was working ok before but today I noticed it has stopped working. 据我所记得,confirm()以前工作正常,但今天我注意到它已经停止工作。 I am using chrome v 62.0 我正在使用Chrome v 62.0

If I change my code to 如果我将代码更改为

 window.onbeforeunload = function (e) {

    return "Do you wish to save changes before continuing? CLick 'OK' for 'Yes' and 'Cancel' for 'No'";

    var model = _selectedClassNodes + ";" + _selectedIndicatorNodes + ";" + _selectedNode + ";" + _pageHeaderId;

    $.ajax({
        url: "@Url.Action("SaveDefaultSettings", "Maps")",
        type: 'get',
        data: { defaultSettings: model },
        success: function (data) {



        }
    });
 };

Instead now a default dialog says "Changes you made may not be saved" Reload/Don't Reload 相反,现在默认对话框显示“您所做的更改可能不会保存”。重新加载/不重新加载

From MDN Web docs 来自MDN Web文档

If a string is assigned to the returnValue Event property, a dialog appears asking the user for confirmation to leave the page (see example below). 如果将字符串分配给returnValue Event属性,则会出现一个对话框,要求用户确认离开页面(请参见下面的示例)。 Some browsers display the returned string in the dialog box, but others display their own message. 一些浏览器在对话框中显示返回的字符串,而其他浏览器则显示自己的消息。

That is why I am seeing a different message instead of the one I am returning. 这就是为什么我看到的是一条不同的消息,而不是我返回的消息。

The point that I mentioned it was working before was a confusion because same confirm dialog message is being used at another button click too. 我之前提到的那点很令人困惑,因为在另一个按钮单击时也使用了相同的确认对话框消息。

My final code is 我的最终代码是

window.onbeforeunload = function (e) {


    var model = _selectedClassNodes + ";" + _selectedIndicatorNodes + ";" + _selectedNode + ";" + _pageHeaderId;

    $.ajax({
        url: "@Url.Action("SaveDefaultSettings", "Maps")",
        type: 'get',
        data: { defaultSettings: model },
        success: function (data) {



        }
    });

    //if some unsaved data exists
    if (editedRows.length > 0) {
        return "There are some changes in grid data. You need to click 'Save Changes' or 'Cancel Changes' button on grid before continuing.";
    }


};

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

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