JavaScript URL parameter read improved version _javascript tips

Source: Internet
Author: User

Here are some of the ways that the cloud-dwelling community has been using

Copy Code code 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>

Here is the regular function
Parsing classes that implement URL links with JavaScript
Http://www.jb51.net/article/15000.htm

The following code is a bit messy.
/* The more concise method of taking URL parameters, described in the JavaScript Authority Guide, is to use a loop without regular expressions. The benefit of returning an object at once is that it only needs to be called once, and the parameter and value pair can exist in one object, and then the value of other parameters will not be called again, as long as the object's properties are taken.
Copy Code code as follows:

Usage
* var args = Getargs (); Parsing parameters from a URL
* var q = args.q | | ""; If a parameter is defined, its value is used, otherwise it is given 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); Takes a query string, such as Truncating a1=v1&a2=&a3=v3 from Http://www.snowpeak.org/testjs.htm?a1=v1&a2=&a3=v3#anchor.
var pairs = Query.split ("&"); Divide the array by & characters
for (var i = 0; i < pairs.length; i++) {
var pos = pairs[i].indexof (' = '); Find "Name=value" on
if (pos = = 1) continue; If not right, jump out of the loop to continue the next pair
var argname = pairs[i].substring (0,pos); Fetch parameter name
var value = pairs[i].substring (pos+1); Take parameter values
Value = decodeuricomponent (value); If necessary, decode
Args[argname] = value; An attribute that is stored as an object
}
return args; returns this object
}

Its outstanding advantage is that the program only to perform a fetch operation, and then repeat the parameter value, do not need to execute the program. The URL parameter is easy and easy to understand.

Here's what I published earlier, "No loops" but "somewhat too complicated" versions:
Copy Code code as follows:

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.
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 you write \?; Well number is dispensable, so the pattern ends with #?
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 '));

Related Article

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.