var reg = /([^=&\s]+)[=\s]*([^=&\s]*)/g
var obj = {}
while(reg.exec(query)){
obj[RegExp.$1] = RegExp.$2
}
return obj
}
var jerry = parseQuery("name=jerry&age=1")
console.log(jerry)
var tom = parseQuery("name= tom &age=12&gender&")
console.info(tom)
js可以通过以下方法获取url地址的参数:1、比如有如下url请求:
http://zhidao.baidu.com/question/586494936989579005.html?entry=business_qb&ishq=1
2、可以用一下方法获取参数:
var params = function(str) {
function urldecode(str) {
return decodeURIComponent((str+'').replace(/\+/g, '%20'))
}
function transformToAssocArray( prmstr ) {
var params = {}
var prmarr = prmstr.split("&")
for ( var i = 0i <prmarr.lengthi++) {
var tmparr = prmarr[i].split("=")
params[tmparr[0]] = urldecode(tmparr[1])
}
return params
}
var prmstr = window.location.search.substr(1)
return prmstr != null &&prmstr != "" ? transformToAssocArray(prmstr) : {}
}()