Getting query string values in javascript

Sometimes we get the important data through query string but question is of getting query string values in javascript or jquery because we need these values at client side. Even we can do it by using only javascript also.

For getting values, we need to parse the query string as follows:

1. Using regular expression in javascript

We need to call it like :

Here we can pass the key name for which we want to parse the value from query string.

2. Using simple javascript

If we want we can get the whole query string as object and then access its value by its key like :

In above given code, steps are as follows :
1. Getting url
2. Parsing query string by splitting url
3. Looping through each and every key in parsed query string
4. Storing every key and its value as an object
5. Return the object

We can call the function and get the object in an variable as follows :

For the query string x=5&y&z=hello&x=6 this returns the object:

3. Using jQuery :

Usage :

You Might Interested In

Leave a Reply

Enclose a code block like: <pre><code>Your Code Snippet</code></pre>.