Dynamic loading JS and CSS (external file) _javascript tips

Source: Internet
Author: User
Copy Code code as follows:

Dynamically loading 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 loading 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 loading 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 browser
Sheet.insertrule (Selectortxt + "{" + Csstxt + "}", position);
}else if (sheet.addrule) {//determine if 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.