JS get (receive) the value of the address bar parameter _javascript tips

Source: Internet
Author: User
When the address bar contains parameters, we can then target the page through Window.location.search to get the parameters and values we need
Copy Code code as follows:

Gets an array of arguments for 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 =/[=|^==]/; Split with =, excluding = =
var Set1 = tmparray[i].replace (Reg, ' & ');
var tmpStr2 = set1.split (' & ');
var array = new Array;
Array[tmpstr2[0]] = tmpstr2[1];
Paramsarray.push (array);
}
}
Returns the array of arguments
return paramsarray;
}
Get parameter values based on parameter names
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;
}

Our current page address is http://www.jb51.net/tatame/admin/EditPosts.aspx?opt=1
You can see that the URL contains a parameter
var opt = GetParamValue ("opt");
alert (opt); pop up to 1.
Copy Code code as follows:

/********
Receive Address bar parameters
Key: Parameter name
**********/
function Getquery (key) {
var search = Location.search.slice (1); Gets a get-way submit query string
var arr = search.split ("&");
for (var i = 0; i < arr.length; i++) {
var ar = arr[i].split ("=");
if (ar[0] = = key) {
return ar[1];
}
}
}

Call Getquery (' test ') on the page load the function is good, very practical

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.