刚刚有人问如何在静态页面获得url的参数,我想通过JS就可以解决这个问题。下面一个简单的例子加以说明。
a.html (传参页面)
b.html(取参页面)
实质就是借助浏览器对url的记忆功能利用Js拆分字符而。
[img]http://dl.iteye.com/upload/attachment/270397/efb399ba-ec25-3b05-bbd6-ae33f8b71d0e.png[/img]
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>传参数页面</title>
</head>
<body>
<form method="post">
<span style="color: #ff0000;"><a href="b.html?<span style="color: #339966;">name</span>=zhangsan&<span style="color: #339966;">sex</span>=男&<span style="color: #339966;">aaa</span>=100">aaa</a></span>
</form>
</body>
</html>
b.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>获取参数页</title>
</head>
<body>
<script type="text/javascript"><!--
function gup( name ) {
<span style="color: #ff0000;">var regexS = "[\\?&]"+name+"=([^&#]*)";//匹配name参数对</span>
var regex = new RegExp( regexS );
<span style="color: #ff0000;"> var results = regex.exec( window.location.href );//过滤超链接,</span>
if( results == null ) {
return "";
} else {
document.getElementById("show").innerHTML = results[1];//显示对应的参数值
<span style="color: #ff0000;"> return results[1]; </span>
}
}
-->
</script>
<button onclick="gup('name')">name</button>
<button onclick="gup('sex')">sex</button>
<button onclick="gup('aaa')">aaa</button>
<div id="show">
</div>
</body>
</html>
实质就是借助浏览器对url的记忆功能利用Js拆分字符而。
[img]http://dl.iteye.com/upload/attachment/270397/efb399ba-ec25-3b05-bbd6-ae33f8b71d0e.png[/img]