使用AJAX动态加栽JS脚本和CSS样式

本文介绍了一种在网页中动态加载JavaScript和CSS文件的方法。通过使用JavaScript创建新的script和link元素,并利用AJAX技术获取远程文件内容,可以实现按需加载资源的功能。此方法有助于优化页面加载速度和提高用户体验。

1.Default.htm

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  
<script src="js.js" type="text/javascript"></script>
  
<title>TestJS</title>
</head>
<body onload="doOnLoad();">
  
<input type="button" id="btnTest" value="Test" disabled="disabled"/>
  
<br />
  
<input type="text" id="txtJSName" value="1" />
  
<input type="button" id="btnGetJS" value="GetJS" />
</body>
</html>

 

 

2.js.js

 

ExpandedBlockStart.gifContractedBlock.giffunction doOnLoad(){
  document.getElementById(
'btnTest').onclick = doTest;
  document.getElementById(
'btnGetJS').onclick = doGetJS;
}


ExpandedBlockStart.gifContractedBlock.gif
function doTest(){
  
var jsTest = test;
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(jsTest){
    jsTest();
    checkBtnTest(
'myjs');
  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
else{
    alert(
'无函数!');
  }

}


ExpandedBlockStart.gifContractedBlock.gif
function doGetJS(){
  checkBtnTest(
'myjs');
  
var jsName = 'js' + document.getElementById('txtJSName').value + '.js';
  AjaxPage(
'myjs', jsName);
}


ExpandedBlockStart.gifContractedBlock.gif
function GetHttpRequest() 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if (window.XMLHttpRequest){//Gecko
    return new XMLHttpRequest();
  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
else if (window.ActiveXObject){//IE
    return new ActiveXObject("MsXml2.XmlHttp");
  }

}


ExpandedBlockStart.gifContractedBlock.gif
function AjaxPage(sId, url){
  
var oXmlHttp = GetHttpRequest();
ExpandedSubBlockStart.gifContractedSubBlock.gif  oXmlHttp.OnReadyStateChange 
= function(){
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if ( oXmlHttp.readyState == 4 ){
ExpandedSubBlockStart.gifContractedSubBlock.gif      
if (oXmlHttp.status == 200 || oXmlHttp.status == 304){
        IncludeJS( sId, url, oXmlHttp.responseText );
      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
else{
        alert(
'XML request error: ' + oXmlHttp.statusText + ' (' + oXmlHttp.status + ')');
      }

    }

  }

  oXmlHttp.open(
'GET', url, true);
  oXmlHttp.send(
null);
}


ExpandedBlockStart.gifContractedBlock.gif
function IncludeJS(sId, fileUrl, source){
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(source != null){
    
var scriptTag = document.getElementById(sId);
    
var oHead = document.getElementsByTagName('HEAD').item(0);
ExpandedSubBlockStart.gifContractedSubBlock.gif    
if(scriptTag){
      oHead.removeChild(scriptTag);
    }

    
var oScript = document.createElement( "script" );
    oScript.language 
= "javascript";
    oScript.type 
= "text/javascript";
    oScript.id 
= sId;
    oScript.defer 
= true;
    oScript.text 
= source;
    oHead.appendChild(oScript);
    checkBtnTest(sId);
  }

}
 

ExpandedBlockStart.gifContractedBlock.gif
function checkBtnTest(sId){
  
var scriptTag = document.getElementById(sId);
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(scriptTag){
    document.getElementById(
'btnTest').setAttribute('disabled'false);
  }

ExpandedSubBlockStart.gifContractedSubBlock.gif  
else{
    document.getElementById(
'btnTest').setAttribute('disabled'true);
  }

}

 

 

3. js1.js 和 js2.js

 

ExpandedBlockStart.gifContractedBlock.giffunction test(){
  alert(
'js1.js');
}


ExpandedBlockStart.gifContractedBlock.gif
function test(){
  alert(
'js2.js');
}

 

 

4. 动态加CSS样式

 

ExpandedBlockStart.gifContractedBlock.giffunction IncludeCSS(sId, fileUrl){
  
var cssTag = document.getElementById(sId);
  
var oHead = document.getElementsByTagName('HEAD').item(0);
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(cssTag){
    oHead.removeChild(cssTag);
  }

  
var oCss = document.createElement('link');
  oCss.id 
= sId;
  oCss.href 
= fileUrl;
  oCss.rel 
= 'stylesheet';
  oCss.type 
= 'text/css';
  oHead.appendChild(oCss);
}

转载于:https://www.cnblogs.com/hawkjin/archive/2008/07/22/1248900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值