金鹏

|| 叶落知秋,见微知著。|| 不出户,知天下﹔不窥牖,见天道。其出弥远,其知弥少。是以圣人不行而知,不见而明,不为而成。

博客园 首页 新随笔 联系 订阅 管理

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

 

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


function doTest(){
  
var jsTest = test;
  
if(jsTest){
    jsTest();
    checkBtnTest(
'myjs');
  }

  
else{
    alert(
'无函数!');
  }

}


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


function GetHttpRequest() 
  
if (window.XMLHttpRequest){//Gecko
    return new XMLHttpRequest();
  }

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

}


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

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

    }

  }

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


function IncludeJS(sId, fileUrl, source){
  
if(source != null){
    
var scriptTag = document.getElementById(sId);
    
var oHead = document.getElementsByTagName('HEAD').item(0);
    
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);
  }

}
 

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

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

}

 

 

3. js1.js 和 js2.js

 

function test(){
  alert(
'js1.js');
}


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

 

 

4. 动态加CSS样式

 

function IncludeCSS(sId, fileUrl){
  
var cssTag = document.getElementById(sId);
  
var oHead = document.getElementsByTagName('HEAD').item(0);
  
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);
}
posted on 2008-07-22 17:30  金鹏  阅读(847)  评论(0编辑  收藏  举报