window.onbeforeunload,window.unload,window.beforeunload函数在IE和Mozilla中不起作用

时间:2016-11-30 08:44:17

标签: javascript jquery angularjs

window.onbeforeunload = function (event) {            
    // I need to call an API here but the window is closing
    // before making that HTTP call
    parent.API.LMSFinish("").then(function () { })
};

还有其他方法可以调用window.onbeforeunload函数吗?

1 个答案:

答案 0 :(得分:1)

您可以在Firefox和IE下使用以下代码:

data.table

更新:

如果您的通话是异步,则该窗口将关闭,您的通话将不会启动。您必须在该事件中执行同步调用,如下所示:

var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable

myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
    var confirmationMessage = 'Are you sure to leave the page?';
    (e || window.event).returnValue = confirmationMessage;
    return confirmationMessage;
});