Method callback: the callback method refers to the process of automatically executing the specified method after a method is executed. the following are two representative examples to illustrate the method callback in the JS world.
Dynamic Loading of JS script files. When loading is complete, callback a function
Copy codeThe Code is as follows:
<Script>
/* Js dynamic script Library Loading Method */
Function include_js (file ){
Var _ doc = document. getElementsByTagName ('head') [0];
Var js = document. createElement ('script ');
Js. setAttribute ('type', 'text/javascript ');
Js. setAttribute ('src', file );
_ Doc. appendChild (js );
If (! /* @ Cc_on! @ */0) {// if not IE
// Firefox2, Firefox3, Safari3.1 +, Opera9.6 + support js. onload
Js. onload = function (){
//... Your code Logic
}
} Else {// IE6, IE7 support js. onreadystatechange
Js. onreadystatechange = function (){
If (js. readyState = 'loaded' | js. readyState = 'complete '){
//... Your code logic // load the Jquery script library, and then execute the methods in jquery
$ ("# Div1" pai.html ("OK ");
}
}
}
Return false;
} // Execution function
Include_js ('HTTP: // img1.c2cedu.com/Scripts/jquery/jquery-1.4.2.min.js ');
</Script>
2. dynamically load the IFRAME framework page. After loading is complete, call back a function.
Copy codeThe Code is as follows:
<Script>
Var iframe = document. createElement ("iframe ");
Iframe. src = http://www.jb51.net;
If (iframe. attachEvent ){
Iframe. attachEvent ("onload", function (){//... Your code logic});} else {
Iframe. onload = function (){
//... Your code Logic
};
}
Document. body. appendChild (iframe );
</Script>