javascript中获取地址栏参数

本文介绍了三种使用JavaScript从URL中获取参数值的方法。方法一通过字符串操作定位并提取参数值;方法二利用数组和循环来解析查询字符串;方法三采用正则表达式匹配的方式获取指定参数。

比如 index.asp?id=xxx&name=xxx

怎么用javascript怎么能取到id的name的值呢?

方法一:

 1None.gif<script>
 2None.giffunction getvalue(name)
 3ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 4InBlock.gif var str=window.location.search;
 5InBlock.gif if (str.indexOf(name)!=-1)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
 7InBlock.gif  var pos_start=str.indexOf(name)+name.length+1;
 8InBlock.gif  var pos_end=str.indexOf("&",pos_start);
 9InBlock.gif  if (pos_end==-1)
10ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
11InBlock.gif   return str.substring(pos_start);
12ExpandedSubBlockEnd.gif  }

13InBlock.gif  else
14ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
15InBlock.gif   return str.substring(pos_start,pos_end)
16ExpandedSubBlockEnd.gif  }

17ExpandedSubBlockEnd.gif }

18InBlock.gif else
19ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
20InBlock.gif  return "没有这个name值";
21ExpandedSubBlockEnd.gif }

22ExpandedBlockEnd.gif}

23None.gifvar strName=prompt("请输入您所要值的名字");
24None.gifalert(getvalue(strName));
25None.gif</script>


方法二:

在eWebEditor在线编辑器里是这样处理的,给你参考一下:

1None.gifvar URLParams = new Array();
2None.gifvar aParams = document.location.search.substr(1).split('&');
3ExpandedBlockStart.gifContractedBlock.giffor (i=0; i < aParams.length  i++)dot.gif{
4InBlock.gif var aParam = aParams.split('=');
5InBlock.gif URLParams[aParam[0]] = aParam[1];
6ExpandedBlockEnd.gif}

7None.gif
8None.gif//取得传过来的name参数
9None.gifname=URLParams["name"];

方法三:
1None.gif<script type="text/javascript">
2ExpandedBlockStart.gifContractedBlock.gifRequest = dot.gif{
3ExpandedSubBlockStart.gifContractedSubBlock.gif QueryString : function(item)dot.gif{
4InBlock.gif  var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)","i"));
5InBlock.gif  return svalue ? svalue[1] : svalue;
6ExpandedSubBlockEnd.gif }

7ExpandedBlockEnd.gif}

8None.gifalert(Request.QueryString("id"));
9None.gif</script>

转载于:https://www.cnblogs.com/meil/archive/2007/04/05/701073.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值