【问题标题】:iFrame OnLoad event in IE not firing after Tapestry Zone updateTapestry Zone 更新后,IE 中的 iFrame OnLoad 事件未触发
【发布时间】:2014-02-25 10:21:02
【问题描述】:

我有一个 Tapestry 区域,里面是一个 <iframe> 元素。当 iframe 完成加载时,我想运行一个简单的 JS 函数(只是隐藏和启用一些东西,没什么特别的)。

在 chrome 和 firefox 中,它工作得很好,但我遇到了 IE 9 的问题。

function afterExportLoad() {
    // hide throbber gif
    // enable submit button
}

所以,自然而然,我尝试像这样(内联)将它绑定到 iframe

<iframe onload="afterExportLoad()" .../>

通过 PrototypeJS

exportFrame.observe('load', afterExportLoad);

通过原生 JS

if (window.addEventListener) {
    exportFrame.addEventListener("load", afterExportLoad, false);
} else if (window.attachEvent) {
    exportFrame.attachEvent("onload", afterExportLoad);
} else {
    exportFrame.onload = afterExportLoad;
}

使用上述任何方式,它都适用于 IE,但在 IE 中,加载 iframe 后,gif 被“冻结”并且按钮仍然被禁用。

有没有办法让它在 IE9(以及可能的任何其他 IE 版本)中工作?

谢谢你:)

【问题讨论】:

    标签: javascript internet-explorer iframe onload


    【解决方案1】:

    所以,我摆弄了一下,得到了这个解决方案:

    在函数中添加了浏览器检查

    function afterExportLoad() {
        if (document.getElementsByName('downloadFrame').length > 0) {
            var exportFrame = document.getElementsByName('downloadFrame')[0];
    
            if ((Prototype.Browser.IE && exportFrame.readyState == "complete") || (!Prototype.Browser.IE)) {
                // my stuff
            }
        }
    }
    

    改变了事件

    <iframe onreadystatechange="afterExportLoad();" ...>
    

    在另一个函数中侦听 iframe 所在的区域更新

    if (document.getElementsByName('downloadFrame').length > 0) {
            var exportFrame = document.getElementsByName('downloadFrame')[0];
            if (!Prototype.Browser.IE) {
                exportFrame.stopObserving('readystatechange', exportLoad);
                exportFrame.onload = exportLoad;
            }
        }
    

    如果有人提出更好的解决方案,请告诉我:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      相关资源
      最近更新 更多