Home  >  Article  >  Web Front-end  >  JS parses url parameters into objects

JS parses url parameters into objects

不言
不言Original
2018-04-24 10:49:403718browse


The content of this article is about JS parsing url parameters into objects. It has certain reference value. Now I share it with everyone. Friends in need can refer to it


Implementation ideas: Please see the log and print results

// url参数解析
function getUrlkey(url) {
  var params = {};
  var urls = url.split("?");                  console.log('1_分割url:', urls)
  var arr = urls[1].split("&");               console.log('2_分割urls[1]:', arr)
  for (var i = 0, l = arr.length; i < l; i++) {
    var a = arr[i].split("=");                console.log(&#39;3_遍历 arr 并分割后赋值给a:&#39;, a[0], a[1])
    params[a[0]] = a[1];                      console.log(&#39;4_a给params对象赋值:&#39;, params)
  }                                           console.log(&#39;5_结果:&#39;, params)
  return params;
}


console.log(6,getUrlkey('http//aaa/txt.php?a =1&b=2&c=3'))

Print result:



##Related recommendations:

How to get url parameters in JS

The above is the detailed content of JS parses url parameters into objects. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn