惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
有赞技术团队
有赞技术团队
Hugging Face - Blog
Hugging Face - Blog
罗磊的独立博客
S
SegmentFault 最新的问题
D
Docker
博客园 - 司徒正美
雷峰网
雷峰网
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
A
About on SuperTechFans
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - Franky
月光博客
月光博客
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
MongoDB | Blog
MongoDB | Blog

博客园 - 金鹏

独立的MySQL数据库需要做配置才能在外部访问到。 Visual Studio 2008简体中文试用版(90天)变成永久正式版的两种方法 [转]VisualSVN 1.5.x 破解方法详解 扫描局域网内电脑,并获得对应的MAC地址和主机名 Zend Studio for Eclipse 环境搭建 巧设路由器 实现电信、网通南北互通 微软官方SQL Server 2008正式中文试用版下载 [转]如何循序渐进向DotNet架构师发展 [转]Asp.net中防止用户多次登录的方法 [转]几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比 [转]七年IT奋斗纪实及感悟 IE8的主页使用IE8访问居然也报JS错误 今天向四川汶川地震灾区捐献了100元,表达自己的心意. 绿豆蛙主题乐园 如何避免出现“终端服务器超出了最大允许连接数” 超越 —— 零点乐队 C# 2005 & .NET 3.0 高级编程(第5版) [转]解开最后期限的镣铐 性能问题依旧困扰着VS 2008
使用AJAX动态加栽JS脚本和CSS样式
金鹏 · 2008-07-22 · via 博客园 - 金鹏

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);
}