javascript中获取地址栏参数
时间:2007-04-20 来源:linxh
本文转自:http://www.cnblogs.com/meil/archive/2007/04/05/701073.html
比如 index.asp?id=xxx&name=xxx
怎么用javascript怎么能取到id的name的值呢?
方法一:
1<script>
2function getvalue(name)
3{
4 var str=window.location.search;
5 if (str.indexOf(name)!=-1)
6{
7 var pos_start=str.indexOf(name)+name.length+1;
8 var pos_end=str.indexOf("&",pos_start);
9 if (pos_end==-1)
10{
11 return str.substring(pos_start);
12 }
13 else
14{
15 return str.substring(pos_start,pos_end)
16 }
17 }
18 else
19{
20 return "没有这个name值";
21 }
22}
23var strName=prompt("请输入您所要值的名字");
24alert(getvalue(strName));
25</script>
2function getvalue(name)
3{
4 var str=window.location.search;
5 if (str.indexOf(name)!=-1)
6{
7 var pos_start=str.indexOf(name)+name.length+1;
8 var pos_end=str.indexOf("&",pos_start);
9 if (pos_end==-1)
10{
11 return str.substring(pos_start);
12 }
13 else
14{
15 return str.substring(pos_start,pos_end)
16 }
17 }
18 else
19{
20 return "没有这个name值";
21 }
22}
23var strName=prompt("请输入您所要值的名字");
24alert(getvalue(strName));
25</script>
方法二:
在eWebEditor在线编辑器里是这样处理的,给你参考一下:
1var URLParams = new Array();
2var aParams = document.location.search.substr(1).split('&');
3for (i=0; i < aParams.length i++){
4 var aParam = aParams.split('=');
5 URLParams[aParam[0]] = aParam[1];
6}
7
8//取得传过来的name参数
9name=URLParams["name"];
方法三:
2var aParams = document.location.search.substr(1).split('&');
3for (i=0; i < aParams.length i++){
4 var aParam = aParams.split('=');
5 URLParams[aParam[0]] = aParam[1];
6}
7
8//取得传过来的name参数
9name=URLParams["name"];
方法三:
1<script type="text/javascript">
2Request = {
3 QueryString : function(item){
4 var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
5 return svalue ? svalue[1] : svalue;
6 }
7}
8alert(Request.QueryString("id"));
9</script>
2Request = {
3 QueryString : function(item){
4 var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
5 return svalue ? svalue[1] : svalue;
6 }
7}
8alert(Request.QueryString("id"));
9</script>
相关阅读 更多 +