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] '); |