iframe's onload is executed twice in Chrome/Opera
<script> <br>var ifr = document.createElement('iframe'); <br>ifr.onload = function(){alert(1);}; <br>document.body.insertBefore(ifr,document.body.childNodes[0]); <br>ifr.src = 'http://www.baidu.com'; <br></script>
< ;/body>
The solution is very simple, just change the order of the code: create an iframe, add it to the body, and finally add the load event. Will behave the same across all browsers.
var ifr = document.createElement('iframe');
document.body.insertBefore(ifr,document.body.childNodes[0]);
ifr.src = 'http://www.baidu.com';
ifr.onload = function() {alert(1);};
In addition, I tested it with Safari 5. There is no alert, it keeps loading, and it can last for more than 30s. Would you like to give it a try?