解析浏览器地址栏查询字符串参数

function getQuery() {
  let search = location.search.length > 0 ? location.search.substr(1) : '';
  if (!search) return null;
  let Query = {};
  if (search.includes('&')) {
    let strArray = search.split('&');
    strArray.forEach(item => {
      if (item.includes('=')) {
        let arr = item.split('=');
        Query[decodeURIComponent(arr[0])] = decodeURIComponent(arr[1])
      }
    })
  } else {
    let arr = search.split('=');
    Query[decodeURIComponent(arr[0])] = decodeURIComponent(arr[1])
  }
  return Query;
}
posted @ 2018-10-24 20:41  aleng_liu  阅读(883)  评论(0)    收藏  举报