iFrame onload JavaScript event

2019-01-07 22:08发布

I have an iFrame, where I want to send a JavaScript command after loading. My current code looks like this:

<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.

4条回答
Evening l夕情丶
2楼-- · 2019-01-07 22:35

document.querySelector("iframe").addEventListener("load", function() {

  this.style.backgroundColor = "red";
  alert(this.nodeName);

});
<iframe src="http://www.example.com/" ></iframe>

查看更多
小情绪 Triste *
3楼-- · 2019-01-07 22:42

@Jasper 's answer was almost correct. It was only missing httpS for the src of the iframe.

HTML:

<iframe src="https://www.example.com/" ></iframe>

JavaScript:

document.querySelector("iframe").addEventListener("load", function() {

  this.style.backgroundColor = "red";
  alert(this.nodeName);

});
查看更多
别忘想泡老子
4楼-- · 2019-01-07 22:48

Use the iFrame's .onload function of JavaScript:

<iframe id="my_iframe" src="http://www.test.tld/">

<script type="text/javascript">
    document.getElementById('my_iframe').onload = function() {
        __doPostBack('ctl00$ctl00$bLogout','');
    }
</script>
查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-07 22:59

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>
查看更多
登录 后发表回答