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

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

黄舟
Release: 2017-04-24 10:19:38
Original
4116 people have browsed it

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;
}
Copy after login

Then we call This function obtains the corresponding parameter value:

var Request = new Object();
Request = GetRequest();
Copy after login

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"));
Copy after login

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!

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