Method _ javascript skills

Source: Internet
Author: User
In the demand for "loading on demand", we often judge how to judge the completion of Script Loading by returning a callback function when the script is loaded? We can use onload to determine the loaded JS object (js. onload), this method is supported by Firefox2, Firefox3, Safari3.1 +, and Opera9.6 + browsers, but not IE6 or IE7. Curve saving-IE6 and IE7 we can use js. onreadystatechange is used to track the status changes (generally loading, loaded, interactive, and complete). When the returned status is loaded or complete, the loading is complete and the callback function is returned.

You need to provide additional instructions on readyState status:
1. In the interactive status, users can participate in the interaction.
2. Opera also supports js. onreadystatechange, but its status is very different from that of IE.

The specific implementation code is as follows:

The Code is as follows:


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 (){
Alert ('firefox2, Firefox3, Safari3.1 +, Opera9.6 + support js. onload ');
}
} Else {
// IE6 and IE7 support js. onreadystatechange
Js. onreadystatechange = function (){
If (js. readyState = 'loaded' | js. readyState = 'complete '){
Alert ('ie6, IE7 support js. onreadystatechang ');
}
}
}

Return false;
}

// Execution function
Include_js ('HTTP: // img.jb51.net/jslib/jquery/jquery-1.3.2.min.js );

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.