Javascript adds the onready overload method to the webpage

Source: Internet
Author: User

The code is mainly transferred from the network, and the source is unknown. I made a slight modification to the code.

 

Code

(Function (){
VaR addevent = function (OBJ, type, FN ){
If (obj. addeventlistener)
OBJ. addeventlistener (type, FN, false );
Else if (obj. attachevent ){
OBJ ["E" + Type + FN] = FN;
OBJ. attachevent ("On" + type, function (){
OBJ ["E" + Type + FN] ();
});
}
};
VaR onready = function (loadevent, waitforimages ){
If (waitforimages ){
Return addevent (window, 'load', loadevent );
}
VaR init = function (){
If (arguments. callee. Done) return;
Arguments. callee. Done = true;
Loadevent. Apply (document, arguments );
};
If (! + "\ V1 "){
If (window. Self = Window. Top ){
(Function (){
Try {
Document.doc umentelement. doscroll ("Left ");
} Catch (e ){
SetTimeout (arguments. callee, 0 );
Return;
}
Init ();
})();
} Else {
Document. attachevent ("onreadystatechange", function (){
If (document. readystate = "complete "){
Document. detachevent ("onreadystatechange", arguments. callee );
Init ();
}
});
}
} Else {
Document. addeventlistener ("domcontentloaded", function (){
Document. removeeventlistener ("domcontentloaded", arguments. callee, false );
Init ();
}, False );
}
Return true;
}
If (! Window. onready ){
Window ["onready"] = onready;
}

})();

 

Load the js code first.

Multiple onready calls can be made on the same page:

 

Code

<HTML>
<Head>
Reference JS

<Script/*... 1 .. */>
Onready (function () {/* Do something */});
</SCRIPT>
</Head>
<Body>

......
<Script/*... 2 .. */>
Onready (function () {/* Do something */});
</SCRIPT>

</Body>
</Html>

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.