The following are several methods that have been used by the home of scripts.
Copy codeThe Code is as follows:
<Script language = "javascript" type = "text/javascript">
Function request (paras ){
Var url = location. href;
Var paraString = url. substring (url. indexOf ("? ") + 1, url. length). split ("&");
Var paraObj = {}
For (I = 0; j = paraString [I]; I ++ ){
ParaObj [j. substring (0, j. indexOf ("= ")). toLowerCase ()] = j. substring (j. indexOf ("=") + 1, j. length );
}
Var returnValue = paraObj [paras. toLowerCase ()];
If (typeof (returnValue) = "undefined "){
Return "";
} Else {
Return returnValue;
}
}
// The following is the application code
Var theurl
Theurl = request ("url ");
If (theurl! = ''){
Location = theurl
}
</Script>
Below are the regular functions
Use Javascript regular expressions to parse url links
Http://www.jb51.net/article/15000.htm
The following code is messy.
/* The JavaScript authority guide introduces a more concise method for getting URL parameters. Use a loop instead of regular expressions. The advantage of returning an object at a time is that you only need to call this function once. The parameter and value pair can exist in an object, and you do not need to call this function after obtaining the value of other parameters, you only need to get the attributes of the object.
Copy codeThe Code is as follows:
* Usage:
* Var args = getArgs (); // parse parameters from URLs
* Var q = args. q | ""; // If a parameter is defined, its value is used. Otherwise, a default value is provided.
* Var n = args. n? ParseInt (args. n): 10;
*/
Var getArgs = function ()
{
Var args = new Object (); // declare an empty Object
Var query = window. location. search. substring (1); // retrieve the query string, such as from http://www.snowpeak.org/testjs.htm? A1 = v1 & a2 = & a3 = v3 # extract a1 = v1 & a2 = & a3 = v3 from anchor.
Var pairs = query. split ("&"); // split it into an array &
For (var I = 0; I <pairs. length; I ++ ){
Var pos = pairs [I]. indexOf ('='); // find the "name = value" pair
If (pos =-1) continue; // if it is not paired, the next pair is continued in the loop.
Var argname = pairs [I]. substring (0, pos); // obtain the parameter name
Var value = pairs [I]. substring (pos + 1); // obtain the parameter value
Value = decodeURIComponent (value); // decoded if needed
Args [argname] = value; // stores an attribute of an object.
}
Return args; // return this object
}
Its outstanding advantage is that the program only needs to perform the extraction operation once, and then repeat the parameter value, no longer need to execute the program. It is easy and easy to get URL parameters.
The following is my previous version of "no loop" but "a little too complex:
Copy codeThe Code is as follows:
// You do not need to use regular expressions to retrieve the parameter values from the URL. The core technology used to replace loops is that the string replace () method can be used as the second parameter by using the function and replaced in a user-defined manner.
// If this parameter is available but has no value, an empty string is returned. If this parameter is not available, undefined is returned.
Var getArg = function (argname)
{
Var str = location. href;
Var submatch;
// Retrieve the question mark from the URL and the query string between the wellhead, such as from http://www.snowpeak.org/testjs.htm? A1 = v1 & a2 = & a3 = v3 # extract a1 = v1 & a2 = & a3 = v3 from anchor.
// The question mark is a special character of the mode, so it must be written \?; The well number is dispensable, so the end Of the pattern is #?
If (submatch = str. match (/\? ([^ #] *) #? /))
{
// Obtain the captured child matching, such as a1 = v1 & a2 = & a3 = v3, add a prefix & rules & a1 = v1 & a2 = & a3 = v3 to facilitate subsequent replacement.
Var argstr = '&' + submatch [1];
// Use a replacement function to replace each group of shapes such as & a1 = v1 with a1: "v1". The attribute declaration is used for object definition.
Var returnPattern = function (str)
{
// $1 and $2 indicate that the 1st and 2nd sub-matches are captured and must be used in strings.
Return str. replace (/& ([^ =] +) = ([^ &] *)/, '$1: "$2 ",');
}
// Execute a global regular expression replacement. The second parameter is the replacement function just defined. Replace a1 = v1 & a2 = & a3 = v3 with a1: "v1", a2: "", a3: "v3 ",
Argstr = argstr. replace (/& ([^ =] +) = ([^ &] *)/g, returnPattern );
// Finally, execute another object declaration, which must be the object declaration in the form of var retvalue = {a1: "v1", a2: "", a3: "v3, there is a comma at the end of the replaced string. just remove the ending comma with substr.
Eval ('var retvalue = {'+ argstr. substr (0, argstr. length-1) + '};');
// Now we get an object. In the URL, each parameter name is its attribute name, and the parameter value is the corresponding attribute value.
Return retvalue [argname];
}
}
// Test
Document. write ('a1 = '+ getArg ('a1') +', a2 = '+ getArg ('a2') + ', a3 = '+ getArg ('a3 '));