javascript无法从列表中删除事件监听器onbeforeunload

时间:2018-01-15 15:45:13

标签: javascript

我尝试了所有可能的方法从window对象中删除事件:

window.onbeforeunload = null;
window.BeforeUnloadEvent = null;
$(window).unbind('beforeunload');
$(window).off('onbeforeunload');
window.removeEventListener('beforeunload', carol, true);
window.removeEventListener('onbeforeunload', carol, true); // carol is that function see image below 

当我输入getEventListener(窗口)时仍然存在

enter image description here

是否有人知道另一种方式或我做错了什么?

1 个答案:

答案 0 :(得分:2)

仅在将事件侦听器作为对象属性进行访问时才使用前导"on"。删除事件时,只需使用事件名称:

 window.removeEventListener('beforeunload', carol, true);