javascript 动态更改 iframe 的加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6765356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:46:55  来源:igfitidea点击:

dynamically change onload for an iframe

javascriptdomiframeonload

提问by Sorin Buturugeanu

I have a page containing a couple of <iframe>tags. I want to change their onload actions dynamically. I have the following code that works fine in FF, Safari, Chrome, Opera, but IE (8) refuses to comply.

我有一个包含几个<iframe>标签的页面。我想动态更改它们的加载操作。我有以下代码在 FF、Safari、Chrome、Opera 中运行良好,但 IE (8) 拒绝遵守。

document.getElementById('myiframe').onload = function() {
    return function() { file_onLoad(data); }
}();

I've been using something similar for setting the onchangeof an <input>element and this works well in all the browsers I've tested, including IE.

我已经使用设置类似的东西一直是onchange一个的<input>元素,这非常适用于所有我测试过的浏览器,包括IE浏览器。

document.getElementById('myinput').onchange = function() {
    return function() { file_onChange(data); }
}();

So I guess it has something to do with the way I'm getting the frame element / object.

所以我想这与我获取框架元素/对象的方式有关。

I've also tried frames['myiframe']but with no success.

我也尝试过,frames['myiframe']但没有成功。

Thanks for your help!

谢谢你的帮助!

回答by u283863

It works fine on mine...
I tried:

它在我的工作正常......
我试过:

function whatever(){
    document.getElementById('myiframe').src="http://www.google.com/"
    document.getElementById('myiframe').onload = function() {
        return function() { alert("Done."); }
    }();
}

and it works. (I tried on IE9 with IE8 mode turned on)
If it does not work for you, try this:

它有效。(我在 IE8 模式下试过 IE9)
如果它不适合你,试试这个:

document.getElementById('myiframe').addEventListener('load', file_onLoad, false);