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?

1
  • 2
    Use the URL.js library Commented Jul 20, 2012 at 15:38

1 Answer 1

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');
Sign up to request clarification or add additional context in comments.

18 Comments

Thanks for a great answer. Added an edit to it to take into account the edge case of parameters with no values.
@DeanMeehan awesome if it "just works" but I would not say that it is a simple solution considering the readability of regex expressions
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...
Would have upvoted if a) explanation of what's going on and b) passes JSHint validation: currently, the [, ""] construction is invalid
How can you get values for an array parameter such as test.com?arr[]=vxcbcvb%20cvbvbcvb
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.