从浏览器的url中获取查询字符串的参数

正则表达式:

1 function getQuery(name){
2     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
3     var r = window.location.search.substr(1).match(reg);
4     if(r != null)
5         return decodeURI(r[2]);
6     return null;
7 }

截取:

 1 function fn() {
 2     var url = decodeURI(window.location.href);
 3     var args = url.split('?');
 4     if (args[0] === url)
 5         return false;
 6     var arr = args[1].split('&');
 7     var obj = {};
 8     for (var i = 0; i < arr.length; i++) {
 9         var arg = arr[i].split('=');
10         obj[arg[0]] = arg[1];
11     }
12     return obj;
13 }

 

posted @ 2019-06-04 15:33  云深一梦,美梦成真  阅读(395)  评论(0编辑  收藏  举报