JS get URL in Parameter value (QueryString) method
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Method One: the Regular method</strong></p></p>function getquerystring (name) {var reg = new RegExp (' (^|&) ' + name + ' = ([^&]*) (&|$) ', ' I '); var r = Window.location.search.substr (1). match (reg); If (r! = Null) {return unescape (r[2]); } return null; This is called: alert (getquerystring ("parameter name 1"));<p><p>Alert (getquerystring ("parameter Name 2"));</p></p><p><p>Alert (getquerystring ("parameter Name 3"));</p></p><p><p><br><br><strong>Method Two: Split Splitting method</strong></p></p>function getrequest () {var url = location.search;//get URL in "?" Character string var therequest = new Object (); If (url.indexof ("?")! =-1) {var str = url.substr (1); STRs = Str.split ("&"); for (var i = 0; I < strs.length; i + +) {therequest[strs[i].split ("=") [0]] = unescape (strs[i].split (" = ") [1]); }} return therequest; } var Request = new Object (); Request = Getrequest (); var parameter 1, parameter 2, parameter 3, parameter n; Parameter 1 = request[' parameter 1 ']; Parameter 2 = request[' parameter 2 '];<br>Parameter 3 = request[' Parameter 3 ']; Parameter n = request[' parameter n '];<p><p></p></p><p><p><strong>Method Three: See also the regular<br></strong>Using JS to get URL parameters, This is often used. For example a url:http://wwww.jb51.net/?q=js, we want the value of the parameter q, which can be called by the following Function.</p></p>function GetQueryString (name) {<span class="Apple-converted-space"><span class="Apple-converted-space"> </span></span><span class="Apple-converted-space"><span class="Apple-converted-space">var reg = new RegExp ("(^|&)" + name + "= ([^&]*) (&|$)", "i"); <span class="Apple-converted-space"> </span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">var r = window.location.search.substr (1). match (reg); </span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">//get URL in "?" Character string and regular match</span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">var context = ""; <span class="Apple-converted-space"> </span></span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">if (r! = null <span class="Apple-converted-space"></span> )</span></span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">context = r[2]; <span class="Apple-converted-space"> </span></span></span></span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">reg = null; <span class="Apple-converted-space"> </span></span></span></span></span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">r = null; <span class="Apple-converted-space"> </span></span></span></span></span></span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">return context = = NULL | | context = = "" | | Context = = "undefined"? "": context; <span class="Apple-converted-space"> </span></span></span></span></span></span></span></span></span><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space"><span class="Apple-converted-space">} alert (getquerystring ("q")); <span class="Apple-converted-space"> </span></span></span></span></span></span></span></span></span></span><p><p></p></p><p><p><strong>Method Four: How to obtain a single parameter</strong></p></p><p><p>function Getrequest () {</p></p><p><p>var url = location.search; Gets the URL in the "?" String after the character</p></p><p><p>If (url.indexof ("?")! =-1) {//to Determine if there are parameters</p></p><p><p>var str = url.substr (1); Start with the first character because the No. 0 one is the number. gets all strings except the question mark</p></p><p><p>STRs = Str.split ("="); Separated by an equal sign (because you know that there is only one argument so you can separate it with an equal sign if you have more than one argument separated by an equal sign &)</p></p><p><p>Alert (strs[1]); Directly pops the first parameter (if more than one parameter is to be looped)</p></p><p><p>}</p></p><p><p>}</p></p><p><p>JS get URL in Parameter value (QueryString) method</p></p></span>
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.