Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 18:50:28
Original
760 people have browsed it

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);
}
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!