Home >Web Front-end >JS Tutorial >Window.onload loaded problems and solutions (Part 1)_javascript skills

Window.onload loaded problems and solutions (Part 1)_javascript skills

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 18:50:28804browse

Solution,
1. Mozilla provides an undocumented function:

Copy code The code is as follows:

// for Mozilla browsers
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", init, false);
}

2. For IE browser, you can use IE’s unique defer attribute:
Copy code The code is as follows:



Script blocks with defer attributes will be executed after the DOM is loaded.
Non-IE browsers will ignore defer and directly execute the script code. Therefore, you can have two methods to prevent non-IE browsers from executing this code for IE:
1. Conditional comments
Copy code The code is as follows:




2. Conditional editing
Copy code The code is as follows:




3. For Safari, here is a jQuery Solution:
Copy the code The code is as follows:

if (/WebKit/ i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
clearInterval(_timer );
init(); // call the onload handler
}
}, 10);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn