Get query string value using JavaScript
Get query string value using JavaScript
Here is the JavaScript function getQuerystring which finds the key form query string and returns the value.
/*
* <summary>
* Get the querystring value
* </summary>
* <param name="key">A string contains the querystring key</param>
* <param name="defaultVal">Object which get returns when there is not key</param>
**/
function getQuerystring(key, defaultVal) {
if (defaultVal == null) {
defaultVal = "";
}
key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
var qs = regex.exec(window.location.href);
if (qs == null) {
return defaultVal;
}
else {
return qs[1];
}
}