JavaScript获取HTML静态页面URL参数传递值的两种方法
作者:像素猫时间:2026-10-28 03:18:08浏览:253
本文介绍了在HTML静态页面中通过JavaScript获取URL参数的两种常用方法。第一种利用正则表达式匹配查询字符串,第二种通过split函数按等号分割字符串。文章提供了完整的代码示例,包括参数提取、赋值及页面间传参的完整流程,并指出了该方法仅适用于GET请求或包含参数的URL,不适用于POST方式。
JavaScript获取URL参数概述
以下代码可直接嵌入到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];
}
// 未找到参数
return "";
} 调用示例如下:
代码如下:
document.getElementById("user").value = getQueryStr("user");
document.getElementById("password").value = getQueryStr("password");
document.getElementById("sysno").value = getQueryStr("sysno"); 例二
方法二:split函数分割字符串
方法二:利用split函数按参数切割成数组
具体代码如下:
<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> 另一个bb.htm的完整示例:
代码如下:
<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>作者最新文章
vtk.js 渲染窗口黑屏或无内容显示的常见原因及排查
2026-11-05 17:30:30
js-cookie 安装与基础用法:set、get、remove 详解
2026-11-05 16:35:06
适合JavaScript初学者的实战项目:制作简易待办事项列表
2026-11-05 16:17:07
JS打开新标签页时怎么通过URL传递参数
2026-11-05 12:21:08
sigma.js 自定义节点样式:设置颜色、大小及标签显示的技巧
2026-11-05 09:55:00
