动态向head中设置javaScript文件
源文件如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script type="text/javascript">
var lastScript;
var h=document.getElementsByTagName("head")[0];
function loadScript(url){
var f=document.createElement("script");
var d=new Date().getTime();//用时间来做为唯一ID
f.type="text/javascript";
f.id=d;
f.src=url+'?'+d;
h.appendChild(f);//将js加入head标签中
if(lastScript&&g(lastScript))
g(lastScript).parentNode.removeChild(g(lastScript));//移除上一次加入的js
lastScript=d;//修改最后一个js的id
alert(h.innerHTML);
}
function g(x){
return document.getElementById(x)
};
</script>
</head>
<body>
<button οnclick="loadScript('http://localhost/alert.js')">Test Alert</button><br />
<button οnclick="loadScript('http://localhost/info.js')">Test Info</button><br />
</body>
</html>