Home  >  Article  >  Web Front-end  >  JS implementation of loading external files and loading IFRMAME. When the loading is completed, specify the pointing method (method callback)_javascript skills

JS implementation of loading external files and loading IFRMAME. When the loading is completed, specify the pointing method (method callback)_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:05:16975browse

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

Copy the code The code is as follows:

<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
Copy the code The code is as follows:

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn