Getting Query parameters in Javascript

pradeep's Avatar author of Getting Query parameters in Javascript
This is an article on Getting Query parameters in Javascript in JavaScript and AJAX.
We sometimes require to get the query parameters of an URL in JavaScript, here's how we can do that.

Code: JavaScript
// get the current URL
 var url = window.location.toString();
 //get the parameters
 url.match(/\?(.+)$/);
 var params = RegExp.$1;
 // split up the query string and store in an
 // associative array
 var params = params.split("&");
 var queryStringList = {};
 
 for(var i=0;i<params.length;i++)
 {
     var tmp = params[i].split("=");
     queryStringList[tmp[0]] = unescape(tmp[1]);
 }
 
 // print all querystring in key value pairs
 for(var i in queryStringList)
     document.write(i+" = "+queryStringList[i]+"<br/>");
Example usage:
HTML Code:
<script language='JavaScript'>
 // get the current URL
 var url = window.location.toString();
 //get the parameters
 url.match(/\?(.+)$/);
 var params = RegExp.$1;
 // split up the query string and store in an
 // associative array
 var params = params.split("&");
 var queryStringList = {};
 
 for(var i=0;i<params.length;i++)
 {
 	var tmp = params[i].split("=");
 	queryStringList[tmp[0]] = unescape(tmp[1]);
 }
 
 // print all querystring in key value pairs
 for(var i in queryStringList)
 	document.write(i+" = "+queryStringList[i]+"<br/>");
 </script>
 
Output:
Code:
ab = pradeep
 site = go4expert
shabbir like this
0
ankit_khandelwal16's Avatar, Join Date: Dec 2010
Newbie Member
Thanks Buddy
It really helped me to solve my problem.
Can you help me more little on this how to make any url from this to get any document of that query
0
rameshb's Avatar, Join Date: Dec 2010
Go4Expert Member
Thank you my friend for the post .
0
ulfemsoy's Avatar, Join Date: Oct 2011
Newbie Member
Nice.

To get a specific named argument in the query you could also do it the following way:
lazerwire.com/2011/10/javascript-get-url-query-argument.html

Remember to url decode the value using the "unescape" method in JavaScript.