拿到一个URL地址获取所有其参数

2018-03-23  本文已影响0人  大勺孙喜贵

//获取URL参数

function getUrlVars(url){

var vars = [];

var baseUrl =''

    var index = url.indexOf('?')

if(index!== -1){

baseUrl = url.slice(0,index)

}

var queryArr = url.slice(index +1).split('&');

for(var i =0;i

var hashIndex =queryArr[i].indexOf('=')

var key =queryArr[i].slice(0,hashIndex)

var value =decodeURIComponent(queryArr[i].slice(hashIndex+1,hashIndex.length))

vars.push(key)

vars[key] =value

    }

return {url :baseUrl,query :vars}

}

var url ='http://wwww.baidu.com?id=123&sed=qere&parter_id=333'

getUrlVars(url)//{url:http://wwww.baidu.com,query : ["id", "sed", "parter_id", id: "123", sed: "qere", parter_id: "333"

上一篇下一篇

猜你喜欢

热点阅读