分解url参数
; (function (global, dt) {
global.search = (function (search) {
var pairs = search.slice(1).split('&')
var result = {}
pairs.forEach(function (pair) {
var idx
if (pair && (idx = pair.indexOf('=')) !== -1) {
var name = pair.slice(0, idx)
//兼容写法
result[name] = result[name.toLocaleLowerCase()] = decodeURIComponent(pair.substr(idx + 1) || '')
}
})
return JSON.parse(JSON.stringify(result))
})(location.search);
})(window, window.document);
执行了以上方法后,访问链接 https://www.baidu.com?a=1&b=2&c=3
获取参数方法
console.log(window.search.a); //1
console.log(window.search.b); //2
console.log(window.search.c); //3
end------------------------------
网友评论