dynamically change onload for an iframe

只愿长相守 提交于 2019-12-07 13:33:38

问题


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.

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

I've been using something similar for setting the onchange of an <input> element and this works well in all the browsers I've tested, including 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.

Thanks for your help!


回答1:


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:

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


来源:https://stackoverflow.com/questions/6765356/dynamically-change-onload-for-an-iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!