js截取url后面的参数

方法一

转成对象

var uId = location.search;  //?id=3&ab=3&cd=4
var obj = {};
//以&为分界切割
var arr =uId.slice(1,uId.length).split('&');//["id=3", "ab=3", "cd=4"]
//遍历数组
arr.forEach(function(val){
        //切割=两边的数据
    var arr1 = val.split('=');
    obj[arr1[0]]=arr1[1];
})

//{id: "3", ab: "3", cd: "4"}


方法二

转成数组

var uId = location.search;
var arr1=[];
var arr =uId.slice(1,uId.length).split('&');
arr.forEach(function(val){
      //删除=前面的数据再放进arr
    arr1.push(val.substr(val.indexOf('=')+1));
//['3','3','4'];
})

你可能感兴趣的:(js截取url后面的参数)