如何删除iFrame添加的beforeunload事件侦听器? 我的情况是我负载添加一些beforeunload事件到DOM的iframe,我想在会话过期(或说某个特定事件)时删除它,并且我不想向用户显示确认消息。那么有没有什么办法可以从iframe中使用javascript删除事件侦听器?任何帮助将不胜感激。如何删除iFrame添加的beforeunload事件监听器?
// parent.html
<!DOCTYPE html>
<html>
<head>
<title>Parent Frame</title>
<script>
window.addEventListener('beforeunload', function(event) {
console.log('I am the 1st one.');
});
window.addEventListener('unload', function(event) {
console.log('I am the 3rd one.');
});
</script>
</head>
<body>
<iframe src="child-frame.html"></iframe>
</body>
</html>
// child.html
<!DOCTYPE html>
<html>
<head>
<title>Child Frame</title>
<script>
window.addEventListener('beforeunload', function(event) {
console.log('I am the 2nd one.');
});
window.addEventListener('unload', function(event) {
console.log('I am the 4th and last one…');
});
</script>
</head>
<body>
☻
</body>
</html>
你需要改变事件侦听器的'child.html'添加的方式 - 我猜你可以改变'child.html'代码,对不对? –