2

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を試しましたが、機能しませんでした。

助けてくれてありがとう、

インバル。

4

1 に答える 1

1

クロス オリジンでは、iframe を制御できない場合があります。iframe ページ内で、その例外をキャッチし、親関数を呼び出して表示できます。

try{
//something
}catch(e){
   window.top.some_public_function(e);
   //window.parent.some_public_function(e) is also work
}
于 2012-07-29T08:51:27.707 に答える