2012-07-29 185 views
2

如何捕捉我的iframe中引發的異常?如何捕獲從iframe中拋出的異常?

這裏是我的代碼:

家長:

 iframe = document.createElement("iframe"); 
     iframe.src = "../a.aspx"; 
     iframe.style.display = "none"; 
     document.body.appendChild(iframe); 

和內部a.aspx代碼:

protected void Page_Load(object sender, EventArgs e) 
    { 
      GenerateKey(); 
      //This function throws an exception, and I want to catch it in the parent window (javascript) 
    } 

我想捕獲異常在頁面打開的IFRAME並顯示一個警告框。 我試過iframe.contentWindow.onerror,但沒有奏效。

感謝您的幫助,

Inbal。

+0

糾正異常錯誤 - 不要嘗試找到解決方案來捕捉它。 – Aristos 2012-07-29 07:13:14

+1

@Aristos - 我想向用戶顯示一個錯誤,但我想在父頁面內執行此操作。這是「更正」。 – Inbal 2012-07-29 07:23:15

+0

當你有異常(捕獲它):呈現一個JavaScript代碼,顯示父母的消息。沒有其他辦法,當你的iframe拋出異常時,父頁面沒有運行。 – Aristos 2012-07-29 07:55:26

回答

1

在你的iframe頁面中,你可以捕獲它的異常並調用父功能來顯示它。

try{ 
//something 
}catch(e){ 
    window.top.some_public_function(e); 
    //window.parent.some_public_function(e) is also work 
}