2016-07-27 85 views
0

我的頁面上有iframe中的頁面(具有不同的域)。但iframed頁有這個代碼iframe中的JS反onbeforeunload提示

<script language="JavaScript"> 
window.onbeforeunload = confirmExit; 
function confirmExit() { 
    return "You have attempted to leave this page. Are you sure?"; 
} 
</script> 

所以我不能關閉父頁面沒有「你有企圖...」的提示。這是非常惱人的提示,任何人都有想法如何阻止這種提示?

編輯:我忘了 - JS在兒童(iframed)頁非常重要。我不能使用沒有JS或表單的使用沙盒模式。

回答

0

這是可能的。您需要在父onbeforeunload事件中清理整個父頁面。這會在調用之前刪除帶有onbeforeunload事件的子iframe。 在你的父頁面添加以下代碼:

<script language="JavaScript"> 
window.onbeforeunload = cleanPage; 
function cleanPage() { 
    document.write(""); 
} 
</script> 
0

由於它是一個跨源請求,因此無法訪問頁面的DOM以除去事件處理程序。

最接近你可能會阻止全部根據this other Stackoverflow question在框架內運行的JavaScript。

+0

對不起,我忘了 - JS在孩子(的iframe)頁面是非常重要的。 –

+0

@MilanObrtlík - 然後,正如我在答案中所說的那樣,你不能。 – Quentin