tyro-util
Version:
26 lines (24 loc) • 653 B
JavaScript
/**
* @desc url参数转对象
* @param {String} url default: window.location.href
* @return {Object}
*/
const parseQueryToObj = url => {
url = !url ? window.location.href : url
if (url.indexOf('?') === -1) {
return {}
}
var search =
url[0] === '?' ? url.substr(1) : url.substring(url.lastIndexOf('?') + 1)
if (search === '') {
return {}
}
search = search.split('&')
var query = {}
for (var i = 0; i < search.length; i++) {
var pair = search[i].split('=')
query[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1] || '')
}
return query
}
module.exports = parseQueryToObj