Home > Web Front-end > JS Tutorial > Dynamically load js and css (external files)_javascript skills

Dynamically load js and css (external files)_javascript skills

WBOY
Release: 2016-05-16 17:36:48
Original
872 people have browsed it
Copy code The code is as follows:

// Dynamically load 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 ){ // Determine non-IE browser
sheet.insertRule( selectorTxt "{" cssTxt "}" ,position ;
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template