JS to the external file loading and the implementation of ifrmame loading, when the load is complete, specify the point method (method callback) _javascript skills

Source: Internet
Author: User

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>

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.