/* Dynamically add JS or Css,url: File path, FileType: File type (JS/CSS) */
function Addjsfiles (url,filetype) {
var ohead = document.getElementsByTagName (' head '). Item (0);
var addheadfile;
if (filetype== "JS") {
addheadfile= document.createelement ("script");
Addheadfile.type = "Text/javascript";
Addheadfile.src=url;
} else{
addheadfile= document.createelement ("link");
Addheadfile.type = "Text/css";
Addheadfile.rel= "stylesheet";
Addheadfile.rev = "stylesheet";
Addheadfile.media = "screen";
Addheadfile.href=url;
}
Ohead.appendchild (Addheadfile);
}
/* Method call * *
addjsfiles ("Js/index.js", "JS");
Addjsfiles ("Css/index.css", "CSS");
When the test finds that the above methods are loaded with files, the files are loaded asynchronously, which can cause errors when the methods or variables in the file are used immediately after loading the file.
So the following method of synchronous loading, when the file is loaded and then to execute the corresponding code or method
Window. import={/* Load a batch of files, _files: Array of file paths, can include js,css,less files, succes: Load success callback function/Loadfilelist:function (_files,succes) {var Filea
Rray=[];
if (typeof _files=== "Object") {filearray=_files;
}else{/* If the file list is a string, use, cut into the array/if (typeof _files=== "string") {Filearray=_files.split (",");
} if (Filearray!=null && filearray.length>0) {var loadedcount=0;
for (var i=0;i< filearray.length;i++) {loadfile (Filearray[i],function () {loadedcount++;
if (loadedcount==filearray.length) {succes (); }}}/* Load js file, url: File path, success: Load successful callback function/function LoadFile (URL, success) {if (!)
Fileisext (Classcodes,url)) {var thistype=getfiletype (URL);
var fileobj=null;
if (thistype== ". js") {fileobj=document.createelement (' script ');
fileobj.src = URL; }eLSE if (thistype== ". css") {fileobj=document.createelement (' link ');
Fileobj.href = URL;
Fileobj.type = "Text/css";
Fileobj.rel= "stylesheet";
}else if (thistype== ". Less") {fileobj=document.createelement (' link ');
Fileobj.href = URL;
Fileobj.type = "Text/css";
Fileobj.rel= "Stylesheet/less"; } success = Success | |
function () {}; Fileobj.onload = Fileobj.onreadystatechange = function () {if (!this.readystate | | ' Loaded ' = = This.readystate | |
' complete ' = = this.readystate) {success ();
Classcodes.push (URL)}} document.getelementsbytagname (' head ') [0].appendchild (Fileobj);
}else{success (); /* Get file type, suffix name, lowercase/function getfiletype (URL) {if (url!=null && url.length>0) {retur N Url.substr (Url.lastindexof (".")).
toLowerCase ();
Return "";
}/* file is loaded/* function Fileisext (filearray,_url) {if (filearray!=null && filearray.length>0) {var len =filea
Rray.length;
for (var i = 0, i < len; i++) {if (Filearray[i] ==_url) {return true;
}} return false;
}} var filesarray=["Js/index.js", "Js/classinherit1.js", "Js/highcharts_2.21.js", "css/index.css"]; Import.loadfilelist (Filesarray,function () {/* * Here Write the code or method required to execute after the load completes);
The above is a small series of JS dynamically loaded JS file and CSS file synchronization/asynchronous two simple way of all content, I hope to help you, a lot of support cloud Habitat Community ~