Method 1: Regular method
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
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.
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)
}
}