我有一个Tapestry区域,里面是一个<iframe>
元素。我想在 iframe 加载完成后运行一个简单的 JS 函数(只是隐藏和启用一些东西,没什么花哨的)。
在chrome和Firefox中,它工作得很好,但是我在IE 9上遇到了问题。
function afterExportLoad() {
// hide throbber gif
// enable submit button
}
所以,天生,我尝试像这样将其绑定到 iframe(内联)
<iframe onload="afterExportLoad()" .../>
通过原型
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版本)中工作?
谢谢:)
所以,我摆弄了一下,得到了这个解决方案:
在函数中添加浏览器检查
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;
}
}
如果任何1想出更好的解决方案,请告诉我:)