Method callback: The callback method refers to the process of automatically executing another specified method after the execution of a method is completed. Here are two representative examples to talk about method callbacks in the JS world.
A pair of JS script files are dynamically loaded. When the loading is completed, call back a function
<script> <br>/* js dynamic loading script library method*/ <br>function include_js(file) { <br>var _doc = document.getElementsByTagName('head')[0]; <br>var js = document.createElement('script'); <br>js.setAttribute('type', 'text/javascript'); <br>js.setAttribute('src', file); <br>_doc .appendChild(js); <br>if (!/*@cc_on!@*/0) { //if not IE <br>//Firefox2, Firefox3, Safari3.1, Opera9.6 support js.onload <br>js.onload = function () { <br>//…your code logic<br>} <br>} else { //IE6, IE7 support js.onreadystatechange <br>js.onreadystatechange = function () { <br>if (js.readyState == 'loaded' || js.readyState == 'complete') { <br>//…Your code logic//Load the Jquery script library. After completion, execute the method in jquery<br>$("#div1").html("ok"); <br>} <br>} <br>} <br>return false; <br>} //execution function <br>include_js('http ://img1.c2cedu.com/Scripts/jquery/jquery-1.4.2.min.js'); <br></script>
Dynamic loading of the IFRAME frame page, when After loading is completed, call back a function
<script> <br>var iframe = document.createElement("iframe"); <br>iframe.src = http://www.jb51.net; <br>if (iframe.attachEvent) { <br>iframe.attachEvent(" onload", function () { // ...your code logic }); } else { <br>iframe.onload = function () { <br>// ...your code logic <br>}; <br>} <br>document.body.appendChild(iframe); <br></script>