检测加载iframe内容的失败 [英] Detect failure to load contents of an iframe

查看:181
本文介绍了检测加载iframe内容的失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以使用 load 事件检测iframe的内容何时加载。不幸的是,为了我的目的,有两个问题:
$ b $ ul

  • 如果加载页面时出现错误(404/500等) ,加载事件永远不会被触发。

  • 如果某些图片或其他依赖项加载失败,则加载事件将照常进行。



  • 有什么方法可以可靠地确定是否发生了上述任何一个错误?

    我正在写一个semi-web semi - 基于Mozilla / XULRunner的桌面应用程序,所以只能在Mozilla中运行的解决方案。

    iframe页面(页面位于相同的域名),一个策略可能如下:

    $ ul
  • 在父文档中,初始化一个变量 var iFrameLoaded = false;

  • 加载iframe文档时,将父变量设置为 true 从iframe文档调用一个父级函数(setIFrame例如:Loaded(); )。
  • 使用 iFrameLoaded >定时器对象(将定时器设置为您的首选超时限制) - 如果标志仍然为false,则可以告知iframe未定期加载。

    我希望这有助于。


    I can detect when the content of an iframe has loaded using the load event. Unfortunately, for my purposes, there are two problems with this:

    • If there is an error loading the page (404/500, etc), the load event is never fired.
    • If some images or other dependencies failed to load, the load event is fired as usual.

    Is there some way I can reliably determine if either of the above errors occurred?

    I'm writing a semi-web semi-desktop application based on Mozilla/XULRunner, so solutions that only work in Mozilla are welcome.

    解决方案

    If you have control over the iframe page (and the pages are on the same domain name), a strategy could be as follows:

    • In the parent document, initialize a variable var iFrameLoaded = false;
    • When the iframe document is loaded, set this variable in the parent to true calling from the iframe document a parent's function (setIFrameLoaded(); for example).
    • check the iFrameLoaded flag using the timer object (set the timer to your preferred timeout limit) - if the flag is still false you can tell that the iframe was not regularly loaded.

    I hope this helps.

    这篇关于检测加载iframe内容的失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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