Common Js,css File Unified Load method (recommended) and call callback function after loading _javascript tips

Source: Internet
Author: User

In order to facilitate resource management and enhance efficiency, the commonly used JS and CSS file loading should be placed in a unified file to complete, but also facilitate the subsequent maintenance of resources. So I use JS to write the following methods, stored in the "sourcecontroller.js" file.

function Sourcecontroller () {var root = ""; var csscount = 0; CSS Load counter var jscount = 0; JS Load counter this.callfunc = NULL;

  callback function this.opt = {Root:root}; This.css = {css0: "Javascript/bootstrap/css/bootstrap.css", Css1: "Css/iconfont/iconfont.css", Css2: "css/

  Common.css "};

  This.script = {script0: "Javascript/jquery.js", Script1: "Javascript/bootstrap/js/bootstrap.js"};
  /* Set file root * @param URL root directory Address * * * this.setroot = function (URL) {root = URL;

  };
  /* Set callback function * @param func callback function */this.setcallback = function (func) {this.callfunc = func;

  }; /* Add script file in Addsource money call * @param URL script file path */this.addscript = function (URL) {if (url = = "" | |
    url = null) return;
  this.script["Script" + object.getownpropertynames (this.script). length] = URL;

  }; /* Add CSS file in addsource money call * @param URL css file path */this.addcss = function (URL) {if (url = = = "" | | url = null) return;
  this.css["CSS" + object.getownpropertynames (THIS.CSS). length] = URL;

  };
    * * Add common resources/This.addsource = function () {Console.log ("Source");
    Addmeta ([["Http-equiv", "x-ua-compatible"],["content", "Ie=edge"]);

    Addmeta ([["Name", "viewport"],["content", "Width=device-width, initial-scale=1"]);
  Createcss (this);

  };
    /* Callback function * @param func callback method * @param obj Current object/function callback (obj) {console.log ("func Run");
  Obj.callfunc (); /* Add <link> Default first load CSS * @param obj current object/function Createcss (obj) {//If there is no CSS file, do not load if (OBJECT.G
    Etownpropertynames (obj.css). Length <= 0) return;
    var url = obj.css["css" + csscount];
    Console.log (URL);
    var css = document.createelement ("link");
    Css.setattribute ("rel", "stylesheet");
    if (Root!= "") {url = root + "/" + URL;
    } css.href = URL;
    document.getElementsByTagName ("Head") [0].appendchild (CSS);
      Css.onload = function () {csscount++; IF (Csscount < Object.getownpropertynames (obj.css). Length) {createcss (obj); }else{//If there is a JS file, start loading JS if (Object.getownpropertynames (obj.script). length > 0) {createjs (obj)
        ;
  }
      }
    }; /* Add <script> JS after loading execute callback function * @param obj current object/function Createjs (obj) {//If no JS file, do not load if (obje
    Ct.getownpropertynames (obj.script). Length <= 0) return;
    var url = obj.script["script" + Jscount];
    Console.log ("Time:" + new Date (). GetTime () + "" + URL);
    var script = document.createelement ("script");
    Script.setattribute ("type", "Text/javascript");
    if (Root!= "") {url = root + "/" + URL;
    } script.src = URL;
    document.getElementsByTagName ("Head") [0].appendchild (script);
      Script.onload = function () {jscount++;
      if (Jscount < Object.getownpropertynames (obj.script). Length) {Createjs (obj);
     }else{//If there is a callback function to execute the callback function if (Obj.callfunc!= null) {     Callback (obj);
    }}/* Add meta * @param attr meta attribute, for two-dimensional array/function Addmeta (attr) {console.log ("meta");
    var meta = document.createelement ("meta");
    for (var i = 0; i < attr.length i++) {Meta.setattribute (attr[i][0], attr[i][1]);
  } document.getelementsbytagname ("Head") [0].appendchild (meta); }
}

The page that needs to be introduced only needs to add the following code in

Window.onload = function () {
  var sc = new Sourcecontroller ();
  Sc.addcss ("Css/index.css"); Additional CSS files must be added to the
  sc.addscript ("Javascript/index.js") before loading the resource, and//additional JS files must be added to the sc.setcallback before loading the resource
  ( function () {
    Document.body.style.display = "block";//Show Body
  });
  Sc.addsource (); Load commonly used JS and CSS files
};

The above is a small series for everyone to bring common Js,css file unified loading method (recommended) and after loading the callback function to call all the content, I hope to help you, a lot of support cloud Habitat Community ~

Related Article

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.