iFrame onload JavaScript 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29233928/
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
iFrame onload JavaScript event
提问by Peleke
I have an iFrame, where I want to send a JavaScript command after loading. My current code looks like this:
我有一个 iFrame,我想在加载后发送一个 JavaScript 命令。我当前的代码如下所示:
<iframe src="http://www.test.tld/" onload="__doPostBack('ctl00$ctl00$bLogout','')">
But with this code the command isn't executed. What must I change to make it work? Only Chrome and Firefox have to be supported.
但是使用此代码,不会执行该命令。我必须改变什么才能使它工作?只需要支持 Chrome 和 Firefox。
回答by Jasper
Use the iFrame's .onloadfunction of JavaScript:
使用.onloadJavaScript的 iFrame函数:
<iframe id="my_iframe" src="http://www.test.tld/">
<script type="text/javascript">
document.getElementById('my_iframe').onload = function() {
__doPostBack('ctl00$ctl00$bLogout','');
}
</script>
<!--OTHER STUFF-->
</iframe>
回答by antelove
document.querySelector("iframe").addEventListener( "load", function(e) {
this.style.backgroundColor = "red";
alert(this.nodeName);
console.log(e.target);
} );
<iframe src="example.com" ></iframe>
回答by technocrusaders.com
Your code is correct. Just test to ensure it is being called like:
你的代码是正确的。只需测试以确保它被调用如下:
<script>
function doIt(){
alert("here i am!");
__doPostBack('ctl00$ctl00$bLogout','')
}
</script>
<iframe onload="doIt()"></iframe>

