動態載入js和css

來源:互聯網
上載者:User
// 動態載入外部js檔案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 );};// 動態載入jsif( flag ){var script = document.createElement( "script" );script.type = "text/javascript";script.text = " ";document.getElementsByTagName( "head" )[0].appendChild( script );};// 動態載入外部css樣式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 );};// 動態載入css樣式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 ){ // 判斷非IE瀏覽器sheet.insertRule( selectorTxt + "{" + cssTxt +"}" ,position );}else if( sheet.addRule ){ //判斷是否是IE瀏覽器sheet.addRule( selectorTxt ,cssTxt ,position )}}

相關文章

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.