简体   繁体   English

onbeforeunload取消绑定不适用于IE

[英]onbeforeunload unbind not working on IE

I am not able to cancel the onbeforeunload in IE. 我无法在IE中取消onbeforeunload。 I am not sure what is the issue but setting it to undefined does not have an effect. 我不确定这是什么问题,但将其设置为undefined不会起作用。 I want to unbind the event under certain condition. 我想在特定条件下取消绑定该事件。 In all other browser it works though. 不过,在所有其他浏览器中都可以。

The code is mentioned below:- 代码如下:

$(document).ready(function(){

    window.onbeforeunload = function(){
        return '';
    }

    window.addEventListener('message', function(e) {
    if(e.data==="UNBIND_UNLOAD"){
        window.onbeforeunload = undefined;
        window.close();
        }
    } , false);

    $('#sendParentMessage').click(function(){
    window.onbeforeunload = undefined;
        window.opener.postMessage('Hi Child How are you!!!','*');
    });
});

Add the events the proper way and remove them 以正确的方式添加事件并删除它们

var unloadFunction = function () {
  return "test";
}
window.addEventListener('beforeunload', unloadFunction)

window.addEventListener('message', function(e) {
if(e.data==="UNBIND_UNLOAD"){
    window.removeEventListener('beforeunload', unloadFunction)
    window.close();
    }
} , false);

or change the code to use a check inside the function instead of removing it 或更改代码以在函数内使用检查而不是将其删除

var isActiveCheck = true
var unloadFunction = function () {
  return isActiveCheck ? "test" : undefined;
}
window.addEventListener('beforeunload', unloadFunction)

window.addEventListener('message', function(e) {
if(e.data==="UNBIND_UNLOAD"){
    isActiveCheck = false
    window.close();
    }
} , false);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM