Home > Web Front-end > JS Tutorial > body text

How to get the query parameters after the browser URL? 2 steps to get it done

php是最好的语言
Release: 2018-08-03 14:06:36
Original
2060 people have browsed it

I encountered a problem while working on a project today, which was to obtain the query parameters behind the browser. I found many methods on the Internet, but they didn’t feel very good, so I wrote one myself.
1. By passing in parameters Get a single query parameter

function getQueryString(name) {
  var value='';
  var reg = new RegExp("(^|&)"+name+"=([^&]*)(&|$)");
  var isQuery=decodeURI(window.location.search).substring(1).match(reg);
  console.log(isQuery);
  if(isQuery!=null){
    value=isQuery[2]
  }
  return value;
Copy after login

}

2. Get all query parameters in the URL

  function getQueryStrings() {
  var data={};
  var parameter=(window.location.search.length>0)?window.location.search.substring(1):0;
  if(parameter!=0){
      var arg=parameter.split('&');
      for(var i=0;i<arg.length;i++){
          var name=decodeURIComponent(arg[i].split("=")[0]);
          var value=decodeURIComponent(arg[i].split("=")[1]);
          data[name]=value;
      }
  }else{
      data=null;
  }
  return data;
Copy after login

}

Related articles:

How to obtain data after URL is encrypted and decrypted

How to obtain the browser type in Js

Related videos:

Browser information

The above is the detailed content of How to get the query parameters after the browser URL? 2 steps to get it done. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template