使用js切割URL的参数

对于一些开发场景,不使用Jsp或freemarker及其其他的模板引擎时,通常通过切割url获得对应的参数,然后通过AJAX与后台交互得到对应的数据

下面是演示实例:

test.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=utf-8" />
<title>切割URL</title> </head> <body>
<a href="/LMS/test?userId=1">点击</a>
</body>
</html>

test2.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=utf-8" />
<title>三级联动效果</title>
<style type="text/css">
select{ width:100px; text-align:center;}
</style>
<script type="text/javascript"> window.onload=function(){ GetRequest();
};
function GetRequest() {
var fullURL = window.location.href;
alert(fullURL);
var url = location.search; //获取url中"?"符后的字串
var theRequest = new Object();
alert(url);
if (url.indexOf("?") != -1) {
var str = url.substr(1);
strs = str.split("&");
alert(strs)
for(var i = 0; i < strs.length; i ++) {
theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
alert(theRequest[strs[i].split("=")[0]]);
}
}
return theRequest;
}
</script>
</head> <body> <p>测试:</p>
</body>
</html>
上一篇:linux文件系统初始化过程(4)---加载initrd(中)


下一篇:一个好用的Log管理类