Javascript获取HTML静态页面参数传递值示例
给大家看一下我的代码 只要把这些代码嵌入到页面文件即可
var LocString = String(window.document.location.href);
function getQueryStr(str) {
var rs = new RegExp("(^|)" + str + "=([^&]*)(&|$)", "gi").exec(LocString), tmp;
if (tmp = rs) {
return tmp[2];
}
// parameter cannot be found
return "";
}
document.getElementById("user").value = getQueryStr("user");
document.getElementById("password").value = getQueryStr("password");
document.getElementById("sysno").value = getQueryStr("sysno");
<script>
urlinfo=window.location.href; //获取当前页面的url
len=urlinfo.length;//获取url的长度
offset=urlinfo.indexOf("?");//设置参数字符串开始的位置
newsidinfo=urlinfo.substr(offset,len)//取出参数字符串 这里会获得类似“id=1”这样的字符串
newsids=newsidinfo.split("=");//对获得的参数字符串按照“=”进行分割
newsid=newsids[1];//得到参数值
alert("您要传递的参数值是"+newsid);
</script>
不过一定要记得 这个方法只是针对含有参数的url有用 ,如果对方用了POST方法传递参数, url中是不会含有参数的所以这个技巧只对GET方法或者指定了参数的url有用哦
下面看一个完整的实例
aa.htm是参数输渗入渗出界面
bb.htm是参数接收处理界面
aa.htm
代码如下:
<html>
<head>
</head>
<body>
<script>
function submit()
{
var input1 = document.getElementById("inputid");
window.open("bb.htm?inputStr=" + input1.value);//传入参数
}
</script>
<input type = "text" id = "inputid">
<input type = "button" onclick = "submit()" value = "提交">
</body>
</html>
bb.htm:
<html>
<head>
<script>
//获得参数的方法
var request =
{
QueryString : function(val)
{
var uri = window.location.search;
var re = new RegExp("" +val+ "=([^&?]*)", "ig");
return ((uri.match(re))?(uri.match(re)[0].substr(val.leng th+1)):null);
}
}
</script>
</head>
<body>
<script>
//调用方法获得参数
var rt = request.QueryString("inputStr");
alert(rt);
</script>
</body>
</html>
<html>
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<SCRIPT LANGUAGE="JavaScript">
<!--
var request = {
QueryString : function(val) {
var uri = window.location.search;
var re = new RegExp("" +val+ "=([^&?]*)", "ig");
return ((uri.match(re))?(uri.match(re)[0].substr(val.leng th+1)):null);
}
}
var a = request.QueryString ("a");
var b = request.QueryString ("b");
var c = request.QueryString ("c");
if ((a != null)){a=a} else{a="参数A空"}
if ((b != null)){b=b} else{b="参数B空"}
if ((c != null)){c=c} else{c="参数C空"}
document.writeln("参数A: " + a);
document.writeln("<br>参数B: " + b);
document.writeln("<br>参数C: " + c);
//-->
</SCRIPT>
</head>
<body>
<form name="form1" action="?">
请输入参数值:<br>
<SCRIPT LANGUAGE="JavaScript">
document.writeln("A:<input type='text' name='a' value='"+a+"'><br>");
document.writeln("B:<input type='text' name='b' value='"+b+"'><br>");
document.writeln("C:<input type='text' name='c' value='"+c+"'><br>");
</SCRIPT>
<input type="submit" name="Submit" value="提交参数查观效果">
</form>
</body>
</html>