Home  >  Article  >  Web Front-end  >  How to get the query parameters after the browser URL? 2 steps to get it done

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

php是最好的语言
php是最好的语言Original
2018-08-03 14:06:362047browse

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;

}

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;

}

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!

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