【问题标题】:Override onbeforeunload popup with my custom popup用我的自定义弹出窗口覆盖 onbeforeunload 弹出窗口
【发布时间】:2017-05-15 11:15:05
【问题描述】:

有什么方法可以覆盖 onbeforeunload 弹出窗口?

我尝试了以下,但它不起作用:

if($('body').hasClass('admin')){
    window.onbeforeunload = function(event) {
        notifySaveChangesUser(event);   
        //return null;
        //event.returnValue = "Say";
    };
}

function notifySaveChangesUser(event){
    //Doing some stuff here
}

【问题讨论】:

    标签: javascript onbeforeunload


    【解决方案1】:

    这个问题已经回答here

    TL;DR:您可以通过从处理程序返回所需的字符串来自定义消息:

    window.onbeforeunload = function() {
        return "You have some unsaved changes";
    };
    

    有关更多详细信息和浏览器怪癖,请参阅上面的答案。

    【讨论】:

    • 我不想默认弹出。我正在使用 apprise 弹出窗口。我需要显示它而不是默认弹出窗口。如果您有任何想法,请告诉我?
    • 好吧,据我所知,在所有现代浏览器的最新版本中,对自定义弹出窗口的支持已被删除。我认为使用默认值是可以的,因为许多大玩家,如 GitHub 或 Google 都使用它并且做得很好。
    猜你喜欢
    • 2015-11-21
    • 2016-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多