在iframe表面发生JavaScript异常 [英] Getting JavaScript exception happening in iframe surface

查看:113
本文介绍了在iframe表面发生JavaScript异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 页面( iframe.html ) ( iframe-content.html )。

  • iframe-content.html 加载在iframe中。

  • 我希望浏览器可以看到异常(例如Firefox错误控制台Firebug)。


    以下是我所看到的:

    $ ol

  • 最初加载 iframe.html ,并用<$ c $加载 iframe-content.html 然而,如果页面是用JavaScript加载的(<$ c>
  • ),那么在firebug中显示JavaScript异常。 $ c> document.getElementById('iframe').src ='iframe-content.html'),则不会显示异常。

    您可以通过以下方式复制:


    1. http://firenet.googlepages.com/iframe.html

    2. 您将看到 iframe-content.html 被加载。

    3. 点击按钮:iframe的内容被再次加载,但是这一次异常不会在Firebug中显示。

    有没有办法在#3处显示异常,而不是被忽略? (你不能在设置src的JS代码中使用try / catch,因为这个代码在页面加载到iframe之前立即返回)。

    解决方案

    似乎你的iframe页面没有真正加载第二次。或者它从缓存中加载,错误被忽略。这很有趣,但我想我找到了解决办法。

      function setContent(){
    try {
    console.log(加载iframe内容);
    document.getElementById('iframe')。src ='iframe-content.html?foo = bar';
    } catch(e){
    console.log(Caught,e);
    }
    console.log(Done loading);



    $ b $ p
    $ b

    我做了什么,是欺骗浏览器,认为我正在加载一个全新的页面作为参数后的url已经改变。

     'iframe-content.html?foo = bar'; 

    您可以用更改的时间戳替换我的栏字符串。当然,这将避免缓存,但它也会迫使它产生你想要的错误。

    Here is the situation:

    • A page (iframe.html) has an iframe loading another page (iframe-content.html).
    • An JavaScript error might happen when iframe-content.html is loaded in the iframe.
    • I'd like that exception to be visible to the browser (e.g. shown in Firefox error console, or Firebug).

    Here is what I see:

    1. When iframe.html is initially loaded, and loads iframe-content.html with src="iframe-content.html", the JavaScript exception shows in Firebug.
    2. However, if the page is loaded in JavaScript (document.getElementById('iframe').src = 'iframe-content.html'), the exception doesn't show.

    You can reproduce this by going to:

    1. http://avernet.googlepages.com/iframe.html with Firefox.
    2. You'll see the exception as iframe-content.html is loaded.
    3. Click on the button: the content of the iframe is loaded again, but this time the exception doesn't show in Firebug.

    Is there a way at #3 to have the exception show, instead of it being silently ignored? (You can't use a try/catch around the JS code that sets the src, as this code returns immediately before the page is loaded in the iframe.)

    解决方案

    It seems that your iframe page is not really loaded on the second time. Or it's loaded from the cache and the error is ignored. This is interesting, but I think I found an way around it.

    function setContent() {
        try {
            console.log("Loading iframe content");
            document.getElementById('iframe').src = 'iframe-content.html?foo=bar';
        } catch (e) {
            console.log("Caught", e);
        }
        console.log("Done loading");
    }
    

    With that the error should appear.

    What I did, was to trick the browser to think that I'm loading a brand new page as the parameters after the url have changed.

    'iframe-content.html?foo=bar';
    

    You could replace my "bar" string with a changing timestamp. Sure, it would avoid the cache, but it would also force it to generate the error like you wished.

    这篇关于在iframe表面发生JavaScript异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆