如何刪除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'代碼,對不對? –