Dynamic Loading of js and css (external files) _ javascript skills

Source: Internet
Author: User
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)
}
}

Related Article

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.