Home > Web Front-end > JS Tutorial > body text

Improved version of JavaScript URL parameter reading_javascript skills

WBOY
Release: 2016-05-16 18:56:29
Original
899 people have browsed it

The following are several methods that Script House has been using

Copy the code The code is as follows:



The following is the regular function
Using Javascript regularity to implement the parsing class of url links
http://www.jb51.net/ article/15000.htm

The following code is a bit messy
/* "The Definitive Guide to JavaScript" introduces a more concise method of getting URL parameters, without regular expressions, using a loop. The advantage of returning an object at one time is that you only need to call this function once. The parameter and value pairs can be stored in an object. You do not need to call this function again to get the values ​​of other parameters in the future. You only need to get the attributes of the object.
Copy code The code is as follows:

* Usage:
* var args = getArgs( ); // Parse parameters from URL
* var q = args.q || ""; // If a parameter is defined, use its value, otherwise give it a default value
* 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); // Get the query string, such as intercepted from http://www.snowpeak.org/testjs.htm?a1=v1&a2=&a3=v3#anchor a1=v1&a2=&a3=v3.
var pairs = query.split("&"); // Separate into arrays with ampersand
for(var i = 0; i < pairs.length; i ) {
var pos = pairs [i].indexOf('='); // Find the "name=value" pair
if (pos == -1) continue; // If there is no pair, jump out of the loop and continue to the next pair of
var argname = pairs[i].substring(0,pos); // Get parameter name
var value = pairs[i].substring(pos 1); // Get parameter value
value = decodeURIComponent(value) ; // If necessary, decode
args[argname] = value; // Save as an attribute of the object
}
return args; // Return this object
}

Its outstanding advantage is that the program only needs to perform an extraction operation once, and it does not need to execute the program again if the parameter value is repeatedly obtained in the future. It is easier to get URL parameters in this way and easier to understand.

The following is the "no loop" but "somewhat too complicated" version I published before:
Copy codeThe code is as follows:

//Use regular expressions to get the parameter values ​​from the URL without looping. The core technology of replacing loops is that the string's replace() method can use a function as the second parameter to replace it in a user-defined manner.
//If there is this parameter name but no value, an empty string is returned; if there is no such parameter name, undefined is returned.
var getArg = function(argname)
{
var str = location.href;
var submatch;
//First take out the query string between the question mark and the wellhead from the URL , such as a1=v1&a2=&a3=v3 cut out from http://www.snowpeak.org/testjs.htm?a1=v1&a2=&a3=v3#anchor.
//The question mark is a special character in the pattern, so it should be written as ?; the pound sign is optional, so the pattern ends with #?
if (submatch = str.match(/?([^#]*) #?/))
{
//Get the captured submatch in the form a1=v1&a2=&a3=v3, add an & in front to make a regular &a1=v1&a2=&a3=v3 for easy replacement in the next step
var argstr = '&' submatch[1];
//Make a replacement function and replace each found group of the shape &a1=v1 with a1:"v1", which is used for object definitions like this Attribute declaration
var returnPattern = function(str)
{
//$1 and $2 represent the first and second captured submatches, which must be used in the string
return str.replace (/&([^=] )=([^&]*)/, '$1:"$2",');
}
//Execute a global regular replacement, the second parameter is just The defined replacement function replaces a1=v1&a2=&a3=v3 with a1:"v1",a2:"",a3:"v3",
argstr = argstr.replace(/&([^=] )= ([^&]*)/g, returnPattern);
//Finally, execute the declaration of an object, which needs to be in the form of var retvalue = {a1:"v1",a2:"",a3:"v3"} ; object declaration, and there is a comma at the end of the string just replaced, just cut off the ending comma with substr
eval('var retvalue = {' argstr.substr(0, argstr.length-1 ) '};');
//Now you have an object. Each parameter name in the URL 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 '));
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