onbeforeunload on safari not displaying the confir

2019-07-21 18:48发布

问题:

I have this portion of code that works on all browsers except on Safari 10.* (iOS and OSX)

The problem is that Safari fires the beforeunload event but it doesn't display on the confirmation message

function goodbye(e) {
    console.log("goodbye");
    en = false;

    e = e || window.event;

    e.cancelBubble = true;
    e.returnValue = msg;

    if (!en) {
        e.preventDefault();
        if(e.stopPropagation)
        e.stopPropagation();
        return e.returnValue;
    }
}

jQuery(window).bind("beforeunload", goodbye);

The good bye message is displayed from the console, so the event is fired on Safari, but the confirmation message (also tried to return confirm(msg); instead of return e.returnValue; ) is not displayed. Spent a lot of hours on that and have seen a lot of questions here on SO but noone seems to have a working solution.

EDIT

it's happening something strange here. If I load the page and refresh once, the message is appearing, then if I leave the page clicking on the leave button and re-refresh it on the same window tab, the message is not appearing.