Home  >  Article  >  Web Front-end  >  Sample code sharing of Get parameter method using JS in HTML page

Sample code sharing of Get parameter method using JS in HTML page

黄舟
黄舟Original
2017-04-24 10:19:384064browse

Here is a JAVASCRIPT client solution for obtaining the URL with QUESTRING parameters, which is equivalent to asp's request.querystring, PHP's $_GET

here is A JAVASCRIPT client solution for obtaining URLs with QUESTRING parameters, which is equivalent to asp's request.querystring, PHP's $_GET

function:

function GetRequest() {
   var url = location.search; //获取url中"?"符后的字串
   var theRequest = new Object();
   if (url.indexOf("?") != -1) { 
      var str = url.substr(1);
      strs = str.split("&");
      for(var i = 0; i < strs.length; i ++) { 
         theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1]);
      }
   }
   return theRequest;
}

Then we call This function obtains the corresponding parameter value:

var Request = new Object();
Request = GetRequest();

var parameter 1, parameter 2, parameter 3, parameter N;

Parameter 1 = Request[''Parameter 1''];

Parameter 2 = Request[''Parameter 2''];

Parameter 3 = Request[''Parameter 3''];

Parameter N = Request[''Parameter N' '];

Use this to obtain the parameter with the same name in the url string

2. Regular analysis method.

function GetQueryString(name) {
   var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)","i");
   var r = window.location.search.substr(1).match(reg);
   if (r!=null) return (r[2]); return null;
}
alert(GetQueryString("参数名1"));
alert(GetQueryString("参数名2"));
alert(GetQueryString("参数名3"));

The above is the detailed content of Sample code sharing of Get parameter method using JS in HTML page. 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