我尝试了所有可能的方法从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(窗口)时仍然存在
是否有人知道另一种方式或我做错了什么?
答案 0 :(得分:2)
仅在将事件侦听器作为对象属性进行访问时才使用前导"on"
。删除事件时,只需使用事件名称:
window.removeEventListener('beforeunload', carol, true);