Solution,
1. Mozilla provides a non-public (unwritable ented) function:
Copy codeThe Code is as follows:
// For Mozilla browsers
If (document. addEventListener ){
Document. addEventListener ("DOMContentLoaded", init, false );
}
2. for IE browsers, you can use the defer attribute unique to IE:
Copy codeThe Code is as follows:
<Script defer type = "text/javascript">
Alert ("DOM loaded! ")
</Script>
Script blocks with the defer attribute are executed after the DOM is loaded.
Non-IE browsers ignore defer and directly execute script code. Therefore, you can use either of the following methods to shield non-IE browsers from executing this code for IE:
1. Condition comments
Copy codeThe Code is as follows:
<! -- [If IE]>
<Script defer type = "text/javascript">
Alert ("DOM loaded! ")
</Script>
<! [Endif] -->
2. Conditional editing
Copy codeThe Code is as follows:
<Script defer type = "text/javascript">
// For Internet Explorer
/* @ Cc_on @*/
/* @ If (@ _ win32)
Alert ("DOM loaded! ");
/* @ End @*/
</Script>
3. For Safari, here is a jQuery solution:
Copy codeThe 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 );
}