Get address URL parameters in JavaScript

Source: Internet
Author: User

The quickest way to JS

JS Code:

The code is as follows Copy Code

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

The URL's parameter & parameter name 1=xxxx& parameter name 2=xxxx& parameter name 3=xxxx

The code is as follows Copy Code

Alert (getquerystring ("parameter name 1"));
Alert (getquerystring ("parameter Name 2"));
Alert (getquerystring ("parameter name 3"));

Then came the improvement, which is compatible with all browsers

The code is as follows Copy Code

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;
}

Use:

How to use

With 2 parameters name and year

The code is as follows Copy Code

Http://localhost:8080/Default.aspx?Name= John &year=2013

All you need to do is:

The code is as follows Copy Code

var name = GetParamValue ("Name");
var year = GetParamValue ("year");

Does it feel a bit like php,asp this kind of OH.

The JS code is a little complicated, and we're using jquery.

If you want to get test, you can introduce the plug-in,
Get the following: var test = $.query.get (' test ');
If the parameter has multiple identical names, you can do this: var arr = $.query.get (' testy ');
Output: [value 1, Value 2, value 3 ...] If you want to get more than one of the same names, you can do this:

  code is as follows copy code

var Arrayelement = $.query.get (' testy[1] ');

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.