Method Callback: The callback method callback refers to the process of automatically executing another specified method when a method is executed. Here are two representative examples of the method callback in the JS world.
A pair of JS script file dynamic loading, when the load is complete, to callback a function
Copy Code code as follows:
<script>
* JS Dynamic Loading Script Library 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//loading jquery script library, completed, executes the jquery method
$ ("#div1"). html ("OK");
}
}
}
return false;
}//execution function
Include_js (' http://img1.c2cedu.com/Scripts/jquery/jquery-1.4.2.min.js ');
</script>
Two dynamically loaded IFRAME frames page, when the load is complete, to callback a function
Copy Code code 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>