JS Dynamic load JS file implementation method

Source: Internet
Author: User

<script language= "Web Effects" type= "Text/javascript"
<!--
Function Jsloader () {
 this.load= function (URL) {
  //gets all <scrcipt> tags
  var ss=document.getelementsbytagname (" Script "); The
  //determines whether the specified file already contains, if it is already included, triggers the Onsuccess event and returns
  for (i=0;i<ss.length;i++) {
    if (ss[i].src && ss[i].src.indexof (URL)!=-1) {
    this.onsuccess ();
    return;
   }
  }
  //creates a script node and sets its properties to be an external JavaScript file
  s=document.createelement ("script");
  s.type= "Text/javascript";
  s.src=url;
  //Gets the head node and inserts <script> into it
  var head=document.getelementsbytagname ("Head") [ 0];
  head.appendchild (s);
  //Gets the reference to the object itself
  var self=this;

  //for IE browsers, use the ReadyStateChange event to determine whether to load successfully
  //for other browsers, use the OnLoad event to determine whether the load was successful
   s.onload=s.onreadystatechange=function () {
   //this pointer in this function refers to an S-node object, not a Jsloader instance,
   //so you have to use self to invoke the Onsuccess event, the same below.
   if (this.readystate && this.readystate== "Loading") return;
   self.onsuccess ();
  }
  s.onerror=function () {
   //If an error occurs, delete the inserted node and trigger the failed event
    Head.removechild (s);
   self.onfailure ();
  }
 };
 //Define Load Success Events
 this.onsuccess=function () {};
 //define load failed events
 this.onfailure= function () {};
}
//Create Object
var jl=new jsloader ();
//define load successful handler
Jl.onsuccess=function () {
 sayhello ();
}
//Define Load Failure handler
Jl.onfailure=function () {
 alert ("File load failed! ");
}
//Start loading
jl.load ("Hello.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.