How to dynamically load javascript files and load javascript files using javascript

Source: Internet
Author: User

How to dynamically load javascript files and load javascript files using javascript

How to dynamically load js files using javascript:
Sometimes we need to dynamically load js files as needed. This chapter will briefly introduce how to implement this function, hoping to bring some help to friends who need it.
I. Common loading methods:

 

<script type="text/javascript" src="mayi.js"></script>

 

The above is a common method for loading js files.

Ii. dynamically create <script> objects:

var head=document.getElementsByTagName("head")[0]; var script=document.createElement("script"); script.type="text/javascript"; script.src="mayi.js"; head.appendChild(script);

The above code can achieve dynamic loading of js files.
However, the above code needs to be supplemented because it is necessary to determine whether the js file has been loaded. The code is modified as follows:

 

var head= document.getElementsByTagName('head')[0]; var script= document.createElement('script'); script.type='text/javascript'; script.onload = script.onreadystatechange=function() {   if(!this.readyState||this.readyState==="loaded"||this.readyState==="complete")   {     help();     script.onload = script.onreadystatechange=null;   } }script.src='mayi.js'; head.appendChild(script);

 

The original address is: http://www.softwhy.com/forum.php? Mod = viewthread & tid = 8815.

For more information, see: http://www.softwhy.com/javascript/

 

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.