最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
js获取地址栏中传递的参数
时间:2017-03-23 编辑:简简单单 来源:一聚教程网
第一种:字符串拆分法
window.location.href 或者 location.href 或者 window.location 获得地址栏中的所有内容
decodeURI()可以解码地址栏中的数据 恢复中文数据
window.search 获得地址栏中问号及问号之后的数据
代码如下 | 复制代码 |
//获取地址栏里(URL)传递的参数 functionGetRequest(value) { //url例子:www.bicycle.com?id="123456"&Name="bicycle"; varurl = decodeURI(location.search);//?id="123456"&Name="bicycle"; varobject = {}; if(url.indexOf("?") != -1)//url中存在问号,也就说有参数。 { varstr = url.substr(1);//得到?后面的字符串 varstrs = str.split("&");//将得到的参数分隔成数组[id="123456",Name="bicycle"]; for(vari = 0; i < strs.length; i ++) { object[strs[i].split("=")[0]]=strs[i].split("=")[1] } } returnobject[value]; } |
第二种:正则匹配法
这种方法其实原理和上一种方法类似,都是从URL中提取,只是提取的方法不同而已。
代码如下 | 复制代码 |
functionGetQueryString(name) { varreg =newRegExp("(^|&)"+ name +"=([^&]*)(&|$)"); varr = window.location.search.substr(1).match(reg); if(r !=null) { returnunescape(r[2]); } returnnull; } |
相关文章
- js获取地址栏里传递的参数 05-15
- HTML简单购物数量小程序代码展示 10-31
- html canvas实现弹幕功能 10-31
- HTML中空格表示的意义 10-31
- html area标签解读 10-31
- html使用表单标签实现注册页面代码展示 10-31