125

Possible Duplicate:
How to get “GET” request parameters in JavaScript?
jQuery querystring
How can I get query string values in JavaScript?

Javascript does not provide a core method to do that, so how to do it?

Community
  • 1
  • 1
David Morales
  • 17,816
  • 12
  • 77
  • 105

1 Answers1

455
function getURLParameter(name) {
  return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search) || [null, ''])[1].replace(/\+/g, '%20')) || null;
}

So you can use:

myvar = getURLParameter('myvar');
Jordan
  • 31,971
  • 6
  • 56
  • 67
David Morales
  • 17,816
  • 12
  • 77
  • 105
  • 1
    Thanks for a great answer. Added an edit to it to take into account the edge case of parameters with no values. – nicolaslara May 27 '14 at 10:35
  • 3
    @DeanMeehan awesome if it "just works" but I would not say that it is a simple solution considering the readability of regex expressions – Adriano Oct 30 '14 at 14:59
  • 3
    Does this work if there is a hash tag in the URI? If there is a `#something` in Chrome, then you have to use `window.location.hash` instead of `window.location.search`... – Jake Wilson Jan 08 '15 at 21:54
  • @Jakobud I'm pretty sure that your browser, if there's a `#` in your URL, would try to get to the anchor of whatever that `#` represents -- so if you went to `example.com/?a=1&b=#2&c4`, it would try to find and go to the anchor `2&c4`. – Nic Jan 15 '15 at 11:53
  • 14
    Would have upvoted if a) explanation of what's going on and b) passes JSHint validation: currently, the [, ""] construction is invalid – Phortuin Feb 02 '15 at 13:08
  • Good, but why not exclude `#` from the allowed chars as well? And why the special handing for the `;` char? Is there sth in the specs for this? :^) – user2173353 Jun 11 '15 at 09:29
  • 1
    How can you get values for an array parameter such as http://test.com?arr[]=vxcbcvb%20cvbvbcvb – mpora Sep 14 '15 at 16:34
  • 1
    I added: if ( window.location.href.indexOf("?") > -1 ) before using the variable. – Garavani Apr 29 '16 at 06:32
  • Can't convert to CoffeeScript because of an "empty slot." – Pete Alvin May 29 '16 at 09:57
  • 1
    I updated this regex to pass jshint and jscs. Enjoy. – Jordan Jun 01 '16 at 16:32
  • Hi Jordan, if I wanted to pass ampersand in example: ?isp=AT&T How would I show it properly on a page without the browser treating the &T as another variable. – user1449384 Sep 15 '16 at 16:56
  • 1
    Works almost perfectly. Except the cases when you have plus character in the url ("+") which is replaced with space by this snippet. – Ingweland Oct 25 '16 at 14:04
  • @user1449384 You need to encode it like you would also encode a space with its hex entitiy `%20`. The ampersand `&` is encoded with `%26`. – kleinfreund Feb 16 '17 at 12:08
  • What about empty parameters? We are not differentiating between empty parameter and absence of parameter. – Shnd May 03 '17 at 07:27
  • Does not work for single character parameter ( &f=blahblah) – Clay Nichols May 23 '17 at 12:31
  • It should be `new RegExp('[?&]'` not necessary to add '|'-character. – phylax Nov 01 '18 at 14:36
  • I have to down vote as the answer is finding a query parameter and NOT a URL parameter. Those are different things. – Jens Mar 09 '21 at 10:39
  • Downvoting because, as pointed out by @Ingweland , it's doing something unfriendly and inscrutible with "+". An explanation of what it's trying to do would go a long way. – Don Hatch Nov 25 '21 at 12:54