html : 引用src参数value=2
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="js/a.js?value=2" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
a();
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
js/a.js: 读出传入参数value=2 ,引用b.js
var js = document.getElementsByTagName("script");
for (var i = 0; i < js.length; i++) {
if (js[i].src.indexOf("a.js") >= 0) {
var arraytemp = new Array();
arraytemp = js[i].src.split('?');
arraytemp = arraytemp[1].split('=');
alert(arraytemp[0] + "=" + arraytemp[1]);
}
}
document.write("<script language='javascript' src='js/b.js'></script>");
function a() {
b();
alert('This is aa!');
}
js/b.js:
function b() {
alert('This is bb!');
}
借助于后台的做法是用后台语言动态根据get过来的参数,动态echo或者response.write出来js代码执行.
<script src="a.php?arg1=1&arg2=2"></script>
这种.
而给js文件传参.则需要根据js文件来写脚本了
关于给js文件传参数, 我给你举个例子
假设,你有a.html
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>new html</title>
<script src="ABC.js?a=1&b=2&c=3"></script>
</head>
<body>
</body>
</html>
你给同一目录下的 ABC.JS传了三个参数.分别是a->1 b->2 c->3
现在你需要在ABC.JS里写这么写
var fName="ABC.js";
var src = document.getElementsByTagName("script")[document.getElementsByTagName("script").length-1].src;
var pat = new RegExp(fName,"i");
var arg = src.split("?")[src.split("?").length-1];
arg = arg.replace(/\s/g, "").split("&");
/*
通过以上处理.好,现在arg=["a=1","b=2","c=3"];
这样就得到了你给js文件传过来的参数了.然后根据参数,自行发挥吧.
*/