JS loads external files and implements loading of IFRMAME. After loading is complete, specify the pointing method (method callback)

Source: Internet
Author: User

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>

Related Article

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.