【JavaScript】查询字符串参数

function getQueryStringArgs() {
  // 取得查询字符串并删除开头的 '?'
  var qs = location.search.length > 0 ? location.search.substring(1) : '',
    // 创建保存数据的对象
    args = {},
    // 取得每一项
    items = qs.length ? qs.split('&') : [],
    item = null,
    name = null,
    value = null

  // 逐一将每一项添加到args对象中
  for (let i = 0; i < items.length; i++) {
    item = items[i].split('=')
    name = decodeURIComponent(item[0])
    value = decodeURIComponent(item[1])

    if (name.length) {
      args[name] = value
    }
  }
  return args
}
posted @ 2020-04-12 11:33  [ABing]  阅读(155)  评论(0编辑  收藏  举报