This article describes in detail how to dynamically load external js files and js files, and dynamically load external css styles and css styles. If you are interested, refer to the following.
The Code is as follows:
// Dynamically load external js files
Var flag = true;
If (flag ){
LoadScript ("js/index. js? 1.1.11 ");
};
Function loadScript (url ){
Var script = document. createElement ("script ");
Script. type = "type/javascipt ";
Script. src = url;
Document. getElementsByTagName ("head") [0]. appendChild (script );
};
// Dynamically load js
If (flag ){
Var script = document. createElement ("script ");
Script. type = "text/javascript ";
Script. text = "";
Document. getElementsByTagName ("head") [0]. appendChild (script );
};
// Dynamically load external css styles
If (flag ){
LoadCss ("css/base.css? 1.1.11 ");
};
Function loadCss (url ){
Var link = document. createElement ("link ");
Link. type = "text/css ";
Link. rel = "stylesheet ";
Link. href = url;
Document. getElementsByTagName ("head") [0]. appendChild (link );
};
// Dynamically load css styles
If (flag ){
Var style = document. createElement ("style ");
Style. type = "text/css ";
Document. getElementsByTagName ("head") [0]. appendChild (style );
Var sheet = document. styleSheets [0];
InsertRules (sheet, "# gaga1", "background: # f00", 0 );
};
Function insertRules (sheet, selectorTxt, cssTxt, position ){
If (sheet. insertRule) {// judge non-ie browsers
Sheet. insertRule (selectorTxt + "{" + cssTxt + "}", position );
} Else if (sheet. addRule) {// determines whether it is an IE browser
Sheet. addRule (selectorTxt, cssTxt, position)
}
}