Copy codeThe Code is as follows: // dynamic loading of external js files
Var flag = true;
If (flag ){
LoadScript ("js/index. js ");
};
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 ");
};
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)
}
}