JavaScript URL parameters two ways

Source: Internet
Author: User

A value that takes a parameter from a URL without having to loop purely with a regular implementation. The core technique for replacing loops is that the replace () method of a string can be used as a second parameter and replaced by a user-defined approach.
Returns an empty string if there is a parameter name but no value, and returns undefined if the parameter name is not available.

Call method: var variable name = getparameter ("parameter name to get", url address)
var r = getparameter ("Age", url);

Test output, the result is: site=popasp tutorial
Alert (R);
Depending on the results obtained, you can use
var pname = r.split ("=") [0]; Gets the name of the parameter
var pvalue = r.split ("=") [1]; Get parameter values

Test output:
Alert ("Parameter name:" + pname + "NN" + "parameter value:" + pvalue);

Other practical applications:
According to the need, with the following methods to achieve their own functions;
var hostname = Location.hostname; Get current domain name (does not contain http://)
var localurl = location.href; Get the current complete URL address information (including http://, domain name, path, specific file and all pass parameters)
var referurl = Document.referrer; Get the complete URL information on the previous page (including http://, domain name, path, specific file and all pass parameters)

var getarg = function (argname)
{
var str = location.href;
var Submatch;
First remove the question mark from the URL and the query string between the wellhead and the Http://www.snowpeak.org/testjs.htm?a1=v1&a2=&a3=v3#anchor a1=v1&a2= &a3=v3.
The question mark is a special character of the pattern, so to write?; The well number is dispensable, so the pattern end is #?
if (Submatch = Str.match (/?) ( [^#]*)#?/))
{
Take a captured child match like A1=v1&a2=&a3=v3, add a & &a1=v1&a2=&a3=v3 to the front to make it easier to replace the next
var argstr = ' & ' +submatch[1];
Replace with a function that replaces each set of shapes such as &a1=v1 with a A1: "V1", a property declaration used by such an object definition
var returnpattern = function (str)
{
Matches the 1th and 2nd children captured by the $ and $ representatives, and must be used in a string
Return Str.replace (/& ([^=]+) = ([^&]*)/, ' $: $ ', '];
}
Performs a global regular substitution, the second parameter is the substitution function just defined, replacing the a1=v1&a2=&a3=v3 with A1: "V1", A2: "", A3: "V3",
Argstr = Argstr.replace (/& [^=]+) = ([^&]*)/g, returnpattern);
The final execution of an object's declaration needs to be shaped like var RetValue = {A1: "V1", A2: "", A3: "V3"}; Object declaration, and the end of the string just replaced with a comma, the end of the comma with substr to intercept
Eval (' var retvalue = {' +argstr.substr (0, argstr.length-1) + '}; ');
Now you get an object with the name of each parameter in the URL, and the value of the parameter is the corresponding property.
return Retvalue[argname];
}
}
Test
document.write (' a1= ' +getarg (' A1 ') + ', a2= ' +getarg (' A2 ') + ', a3= ' +getarg (' A3 '));

The following uses the regular processing method

function Geturlparam (name) {
var reg = new RegExp (^|&) + name + = ([^&]*) (&|$));
var r = window.location.search.substr (1). Match (REG);
if (r!= null)
return unescape (r[2]);
return null;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.