How to judge js script completion _ javascript skills

Source: Internet
Author: User
This article mainly introduces how to judge the completion of js Script Loading. If you need it, you can refer to it and hope to help you in the demand for "loading on demand, we often judge that a callback function is returned when the script is loaded. How can we determine whether the script is loaded successfully?

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 Code is as follows:


Script

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;
}


Include_js ('HTTP: // www.planabc.net/wp-includes/js/jquery/jquery.js ');
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.