Home > Web Front-end > JS Tutorial > 4 ways to share the parameter value (QueryString) in the URL using JS_javascript skills

4 ways to share the parameter value (QueryString) in the URL using JS_javascript skills

WBOY
Release: 2016-05-16 16:52:25
Original
1466 people have browsed it

Method 1: Regular method

Copy code The code is as follows:
function getQueryString(name) {
var reg = new RegExp('(^|&)' name '=([^&]*)(&|$)', 'i');
var r = window .location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
// Call like this:
alert(GetQueryString("Parameter name 1"));

alert(GetQueryString("Parameter name 2"));

alert(GetQueryString("Parameter name 3"));



Method 2: split method

Copy code The code is as follows:
function GetRequest() {
var url = location.search; //Get the url after the "?" character String of
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]] = unescape(strs[i].split("=")[1]);
}
}
return theRequest;
}
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'];

Method 3: See also regular rules

Get url parameters through JS, this is often used. For example, for a URL: http://wwww.jb51.net/?q=js, if we want to get the value of parameter q, we can call it through the following function.

Copy code The code is as follows:

function GetQueryString(name) {
var reg = new RegExp("(^|&)" name "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match (reg); //Get the string after the "?" character in the url and match it regularly
var context = "";
if (r != null)
context = r[2];
reg = null;
r = null;
return context == null || context == "" || context == "undefined" ? "" : context;
}
alert (GetQueryString("q"));

Method 4: Obtaining a single parameter

function GetRequest() {
var url = location.search; //Get the string after the "?" character in the url
if (url.indexOf("?") != -1) { //Determine whether there are parameters
var str = url.substr(1); //Start from the first character because the 0th one is the ? sign to get all strings except question marks
strs = str.split ("="); // Separate with an equal sign (because you know there is only one parameter, so use the equal sign to separate directly. If there are multiple parameters, use the & sign and then the equal sign to separate them)
alert(strs[ 1]); //Pop up the first parameter directly (if there are multiple parameters, it will be looped)
}
}

Related labels:
js
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