Method 1: Regular Expression
Copy codeThe 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 this method as follows:
Alert (GetQueryString ("parameter name 1 "));
Alert (GetQueryString ("parameter name 2 "));
Alert (GetQueryString ("parameter name 3 "));
Method 2: split
Copy codeThe Code is as follows: function GetRequest (){
Var url = location. search; // get the url "? "Character String
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 the Regular Expression
Get url parameters through JS, which is often used. For example, a url: http://wwww.jb51.net /? Q = js. If you want to get the value of q, you can call the following function.
Copy codeThe Code is as follows:
Function GetQueryString (name ){
Var reg = new RegExp ("(^ | &)" + name + "= ([^ &] *) (& | $)", "I ");
Var r = window. location. search. substr (1). match (reg); // obtain "? "Character string and regular match
Var context = "";
If (r! = Null)
Context = r [2];
Reg = null;
R = null;
Return context = null | context = "" | context = "undefined "? "": Context;
}
Alert (GetQueryString ("q "));
Method 4: obtain a single parameter
Function GetRequest (){
Var url = location. search; // get the url "? "Character String
If (url. indexOf ("? ")! =-1) {// determine whether a parameter exists
Var str = url. substr (1); // starting from the first character, because 0th are? Get all operators except Question mark
Strs = str. split ("="); // use the equal sign to separate (because only one parameter is known, use the equal sign to separate multiple parameters with the equal sign)
Alert (strs [1]); // The first parameter is displayed directly (if there are multiple parameters, it must be cyclic)
}
}