Dynamically loading JS, CSS code

Source: Internet
Author: User

First, native JS:

 1/** 2 * Load JS and CSS file 3 * @param jsondata.path prefix path 4 * @param j         Sondata.url need to load the JS path or CSS path 5 * @param jsondata.type need to load the type JS or CSS 6 */7 function Loadwritefiles (Jsondata) 8 {9 Jsondata.path = jsondata.path! = undefined ? Jsondata.path: "", "if" (Jsondata.type = = "JS") one by one {Document.writeln ("<scri PT type= ' Text/javascript ' src= ' + jsondata.path + jsondata.url+ ' ></script> ');}14 el Se if (jsondata.type = = "css") {Document.writeln ("<link rel= ' stylesheet ' href= '" +jsond  Ata.path + jsondata.url+ "' type= ' text/css '/>"); 17}18}19 20/**21 *                Load JS or CSS into head in * @param jsondata.path prefix path * @param jsondata.url JS path that needs to be loadedRADIUS or CSS path * @param jsondata.type the type that needs to be loaded JS or css25 */26 function Loadfi              Lestohead (jsondata) {Jsondata.path = Jsondata.path! = undefined? Jsondata.path: ""; 29                  if (Jsondata.type = = "JS")-{-Var _js = document.createelement ("script"); 32 _js.setattribute ("type", "Text/javascript"), _js.setattribute ("src", Jsondata.path + jsondata. URL); _js.onload = _js.onreadystatechange=function () {if (!this.readystate| | this.readystate== ' Loaded ' | | this.readystate== ' complete ') {if ("function" = = typeof (jsondata["callback"]) && Jsonda                      Ta["Callback"]) {PNS jsondata["callback"].call (this); 38}39  } _js.onload=_js.onreadystatechange=null;               41} 42   document.getElementsByTagName ("Head") [0].appendchild (_JS);//Append to head tag within}44 else if (jsondata . Type = = "css") {_css var = document.createelement ("link"); _js.seta Ttribute ("type", "Text/css"); _css.setattribute ("rel", "stylesheet"); _css.setattrib Ute ("href", Jsondata.path + Jsondata.url), document.getElementsByTagName ("Head") [0].appendchild (_CSS); /Append to head tag 51}52}

Two, jquery version:

Returning results with deferred objects

 1 var uiload = Uiload | | {};  2 3 (function ($, $document, uiload) {4 "Use strict"; 5 6 var loaded = [], 7 promise = False, 8 deferred = $. Deferred (); 9 Uiload.load = function (SRCS) {One srcs = $.isarray (SRCS)? Srcs:srcs.split (/\s+/); if (!promis E) {promise = Deferred.promise ();}15 $.each (SRCs, function (index, SRC) {Promi         SE = Promise.then (function () {return Src.indexof ('. css ') >=0? Loadcss (SRC): loadscript (SRC); 19 }); Deferred.resolve (); return promise;23};24 var loadscript = function (src) {2 6 if (LOADED[SRC]) return loaded[src].promise (); var deferred = $. Deferred (); var script = $document. createelement (' script '); script.src = src;31 script.onload = function (e) {deferred.resolve (e);};34 script.onerror = function (e) {D Eferred.rejecT (e);};37 $document. Body.appendchild (script), loaded[src] = deferred;39 + return def Erred.promise ();};42-var loadcss = function (href) {(loaded[href]) return Loaded[href].promise ( ); var deferred = $. Deferred (); var style = $document. createelement (' link '); style.rel = ' stylesheet '; Style.typ         E = ' text/css '; style.href = href;51 Style.onload = function (e) {deferred.resolve (e); 53 };54 style.onerror = function (e) {deferred.reject (e);};57 $document. Hea D.appendchild (style); Loaded[href] = deferred;59 return deferred.promise ();}62 () (JQuery, D Ocument, Uiload);

Dynamically loading JS, CSS code

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.