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

后端 未结 2 1670
借酒劲吻你
借酒劲吻你 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:53

    I only recently realized that Chrome had changed the behavior of onbeforeunload. I found a workaround that works for the main browsers (tested in Chrome, Firefox and IE, the jsFiddle for some reason doesn't work in Firefox, but my website does). Using jQuery UI, you can make a dialog window come up when leaving the page that gives more info about why leaving the current page would be a problem.

    window.onbeforeunload = function (e) {
        $('
    A search is in progress, do you really want to stop the search and close the tab? If not, choose "Stay on page" on the browser alert.
    ').dialog({ modal:true, close: function(){$(this).dialog('destroy').remove();} }); return "Random message to trigger the browser's native alert."; }

    jsFiddle here

提交回复
热议问题