Summary of several methods for JS to Obtain Address Bar Parameters

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Script>
// Obtain the parameter array in the address bar
Function getUrlParams (){
Var search = window. location. search;
// Write data dictionary
Var tmparray = search. substr (1, search. length). split ("&");
Var paramsArray = new Array;
If (tmparray! = Null ){
For (var I = 0; I <tmparray. length; I ++ ){
Var reg =/[= | ^ =]/; // use = to split, but not =
Var set1 = tmparray [I]. replace (reg ,'&');
Var tmpStr2 = set1.split ('&');
Var array = new Array;
Array [tmpStr2 [0] = tmpStr2 [1];
ParamsArray. push (array );
}
}
// Return the parameter Array
Return paramsArray;
}

// Obtain the parameter value based on the parameter name
Function getParamValue (name ){
Var paramsArray = getUrlParams ();
If (paramsArray! = Null ){
For (var I = 0; I <paramsArray. length; I ++ ){
For (var j in paramsArray [I]) {
If (j = name ){
Return paramsArray [I] [j];
}
}
}
}
Return null;
}

Function test ()
{
Alert ("v =" + getParamValue ("name "));
}

Var test1 = function ()
{
// Alert ("v1 =" + getParamValue ("name "));
// Alert ("v2 =" + GetUrlParam ("name "));
// Alert ("v3 =" + GetUrlParms ("name "));
Alert ("v4 =" + getQuery ("name "));
}

Function GetUrlParam (paramName ){
Var url = document. URL; // URL parameter, which can be obtained by using document. URL. There are too many methods.
Var oRegex = new RegExp ('[\? &] '+ ParamName +' = ([^ &] +) ',' I ');
// Var oMatch = oRegex.exe c (window. top. location. search); // obtain the URL of the current window.
Var oMatch = oRegex.exe c (url );
If (oMatch & oMatch. length> 1)
Return oMatch [1]; // return Value
Else
Return '';
}

// Obtain url parameters
Function GetUrlParms (name ){
Var reg = new RegExp ("(^ | \\? | &) "+ Name +" = ([^ &] *) (\ s | & | $) "," I ");
If (reg. test (location. href ))
Return unescape (RegExp. $2. replace (/\ +/g ,""));
Return "";
}
Var getQuery = function (I ){

Var j = location. search. match (new RegExp ("[? &] "+ I +" = ([^ &] *) (&?) "," I "));

Return j? J [1]: j

};
</Script>

JS: <br>

<A href = "#" onclick = "test1 ();"> obtain the parameter value </a>

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.