We sometimes require to get the query parameters of an URL in JavaScript, here's how we can do that.
Example usage:
Output:
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/>");
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>
Code:
ab = pradeep site = go4expert
shabbir
like this
