js截取url中参数值

有个url如下:

http://passport.csdn.net/account/login?from=http%3a%2f%2fwrite.blog.csdn.net%2fpostedit

我们该如何获取from这个参数的值呢?在网上搜了下方法很简单,如下,第一种是通过正则,第二种通过切串放进数组的方式:

方式一:

function getQueryString(name) {
     var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
     var r = window.location.search.substr(1).match(reg);
     if (r != null) return unescape(r[2]); 
     return null;
}
 console.log( getQueryString("aaa") );

方式二:

 1 function GetRequest() {
 2    var url = location.search; //获取url中"?"符后的字串
 3    var theRequest = new Object();
 4    if (url.indexOf("?") != -1) {
 5        var str = url.substr(1);
 6        strs = str.split("&");
 7        for(var i = 0; i < strs.length; i ++) {
 8            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
 9        }
10     }
11     return theRequest;
12 }
13 var req = GetRequest();
14 var from = req['from'];
15 console.log(from);

 

参考文档:https://blog.csdn.net/tjcyjd/article/details/44275531

posted @ 2018-08-02 16:43  米虫的小圈子  阅读(405)  评论(0编辑  收藏  举报