Copy codeThe Code is as follows:
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> iframe onload is executed twice in Chrome/Opera </title>
</Head>
<Body>
<Script>
Var ifr = document. createElement ('iframe ');
Ifr. onload = function () {alert (1 );};
Document. body. insertBefore (ifr, document. body. childNodes [0]);
Ifr. src = 'HTTP: // www.baidu.com ';
</Script>
</Body>
</Html>
The solution is simple. Just change the code order: Create an iframe, add it to the body, and add the load event. All browsers are consistent.
Copy codeThe Code is as follows:
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, the Safari5 test is used. Without alert, it is always loading and can last for more than 30 s. Let's try it out?