iFrame onload JavaScript event

为君一笑 提交于 2019-11-26 12:28:48

问题


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.


回答1:


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>
    <!--OTHER STUFF-->
</iframe>



回答2:


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

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

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



回答3:


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>


来源:https://stackoverflow.com/questions/29233928/iframe-onload-javascript-event

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