Alert Before Page Close: How to change the Chrome's default message?

后端 未结 2 1648
借酒劲吻你
借酒劲吻你 2021-01-02 18:30

I am using the following code snippet to trigger an alert before page closes but Chrome seems to ignore the message and displays its default message \"Do you want to leave t

2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 18:42

    Update: Deprecated in Chrome : https://developers.google.com/web/updates/2016/04/chrome-51-deprecations?hl=en

    Try the following:

    window.onbeforeunload = function (e) {
        e = e || window.event;
    
        // For IE and Firefox prior to version 4
        if (e) {
            e.returnValue = 'A search is in progress, do you really want to stop the search and close the tab?';
        }
    
        // For Safari
        return 'A search is in progress, do you really want to stop the search and close the tab?';
    };
    

    Message I get in Chrome (both custom and default):

    A search is in progress, do you really want to stop the search and close the tab?

    Are you sure you want to leave this page?

    Source jsFiddle

提交回复
热议问题