Js to get parameters in the url

Source: Internet
Author: User

In some cases, we will use js with parameters. For more information about this code, see.

// Lastest:
Var getArgs = function () {// get url querystring
Var params = document. location. search, reg = /(? : ^? | &)(.*?) = (.*?) (? = & | $)/G, temp, args = {};
While((temp1_reg.exe c (params ))! = Null) args [temp [1] = decodeURIComponent (temp [2]);
Return args;
};
// Get only one:
Var queryString = function (key ){
Return (document. location. search. match (new RegExp ("(? : ^ \? | &) "+ Key +" = (.*?) (? = & | $) ") | ['', Null]) [1];
}
Var args = getArgs ();
Alert (args. name + "|" + args. sex + "|" + args. age );
// Test link: <a href = "? Name = abc & sex = male & age = 12 "> test getQueryString

// Deprecated
Var getArgs = function (){
Var params = document. location. search. substr (1), reg = /(? : ^ | &)(.*?) = (.*?) (? : & | $)/G, temp, args = {};
While((temp1_reg.exe c (params ))! = Null ){
Args [temp [1] = temp [2];
Reg. lastIndex-= 1; // minus 1, matching from the end
}
Return args;
};
Var getArgs = function (){
Var sarg = document. location. search. substr (1), rv = {};
Rv. filename = document. location. pathname. replace (/. * // ig ,'');
If (! Sarg) {return rv ;}
Var consumer G = sarg. split ('&'), atmp = [];
For (var I = 0; I <symbol G. length; I ++ ){
Atmp = aarg [I]. split ('= ');
Rv [atmp [0] = unescape (atmp [1]);
}
Return rv;
};

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.