1. JS natif:
/ *** Charger les fichiers JS et CSS * @param jsondata.path PRAIL PRAIL * @param jsondata.url le chemin JS ou le chemin CSS qui doit être chargé * @param jsondata.type le type qui doit être chargé JS ou CSS * / Function LoadwriteFiles (JSondata) {JSondata.path = jSondat. indéfini? jsondata.path: ""; if (jSondata.type == "js") {document.writeln ("<script type = 'text / javascript' src = '" + jsondata.path + jsondata.url + "'> </cript>");} else if (jSondata.type == "CSS") {document.writeln ("<link rel = 'Stylesheet' href = '" + jsondata.path + jSondata.url + "' type = 'text / css' />");}} / *** chargement js ou css dans la tête * @param jsondata.path prefix path * @param jsondata.urm the jsondata.path prefix path path * @param jsondata the jsondata.path prefix pathre Path CSS qui doit être chargé * @param jSondata.type Le type js ou css * / fonction qui doit être chargé de chargement de chargement (jSondata) {jSondata.path = jSondata.path! = Undefined? jsondata.path: ""; if (jSondata.type == "js") {var _js = document.createelement ("script"); _ jssetAttribute ("type", "javascript"); _ jssetAttribute. _Js.OnreadyStateChange = function () {if (! this.readystate || this.readystate == 'chargé' || this.readystate == 'complet') {if ("function" == typeof (jSondata ["Callback"]) && jSondata ["Callback"]) {jSondata ["Callback"]. _js.onload = _js.OnreadyStateChange = null; } document.getElementsByTagName ("Head") []. AppendChild (_JS); // ajouter à la balise de tête} else if (jSondata.type == "CSS") {var _css = document.CreateElement ("link"); _ jsSetAtTrribute ("type", "text / css"); _ CSS. "Stylesheet"); _ css.setAttribute ("href", jsondata.path + jSondata.url); document.getElementsByTagName ("Head") []. APPENDCHILD (_CSS); // APPENDER TO HEAD TAG}}2. Version jQuery:
Utilisez un objet différé pour retourner le résultat
var uiload = uiload || {}; (function ($, $ document, uiload) {"Use strict"; var chargé = [], promesse = false, deferred = $ .deferred (); uiload.load = function (srcs) {srcs = $ .isArray (srcs)? src: srcs.split (// s + /); if (! Deferred.promise ();} $. Chaque (srcs, fonction (index, src) {promest = promest.then (function () {return src.indexof ('. css')> =? Loadcss (src): loadScript (src);});}); deferred.revelve (); return promed;}; {if (chargé [src]) return chargé [src] .promise (); var deferred = $ .deferred (); var script = $ document.createElement ('script'); script.src = src; script.onerror = function (e) {deferred.resolve (e);}; script.onerror = function (e) {Deferred.Reject (e);}; $ document.body.appendChild (script); chargé [src] = report; return deferred.promise ();}; var loadcss = function (href) {if (hreded [href]) return chargé [href] .promise (); var redered = $ .Deferred (); $ document.CreateElement ('link'); style.rel = 'Stylesheet'; style.type = 'text / css'; style.href = href; style.onerror = function (e) {deferred.resolve (e);}; style.onerror = function (e) {deferred.reject (e);}; $ document.head. = différé; return Deferred.Promis ();}}) (jQuery, document, uiload);Ce qui précède est l'exemple de code pour le chargement dynamique JS et CSS qui vous sont présentés par l'éditeur. J'espère que cela vous sera utile!