weixin_33694620 2017-11-27 02:01 采纳率: 0%
浏览 26

卸载前确认对话框

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.

  1. If there is any unsaved data save it. Ask yes/no to save it (using 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.

As far as I remember confirm() was working ok before but today I noticed it has stopped working. I am using 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

  • 写回答

1条回答 默认 最新

  • 叼花硬汉 2017-11-27 02:31
    关注

    From MDN Web docs

    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). 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.";
        }
    
    
    };
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c