url获取参数

方法一

  var url = location.search; //获取url中"?"符后的字串
    if (url.indexOf("?") != -1) {   
        var str = url.substr(1) //从第一个字符开始 因为第0个是?号 获取所有除问号的所有符串
        strs = str.split("&")  
        var obj = {}
        for(var i = 0; i < strs.length; i++){
            var str1 = strs[i].split('=')
            obj[str1[0]] = str1[1]
            var newarr = []
            newarr.push(obj)
        } 
        var id = newarr[0].id         //获取到id
        var cityname = decodeURI(newarr[0].cityname)
    }

方法二

const parseQuery = () => {
  const search = window.location.search;
  let query = {};
  search
    .slice(1)
    .split("&")
    .forEach((it) => {
      const [key, value] = it.split("=");
      query[key] = decodeURIComponent(value);
    });
  return query;
};

 

posted @ 2019-08-15 13:27  番茄西红柿u  阅读(652)  评论(0编辑  收藏  举报