Unfortunately there is no method in JavaScript for parsing the querystring to get the value. So we try with regular expression to solve this problem..
JavaScript method for get URL parameter(QueryStrings)
function getQuerystring(key, default_)The following javascript code snippet facilitates Javascript's built in regular expressions to retrieve value of the key. Optionally, you can specify a default value to return when key does not exist.
{
if (default_==null) default_="";
key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
var qs = regex.exec(window.location.href);
if(qs == null)
return default_;
else
return qs[1];
}
Example:
The getQuerystring function is simple to use. Let's say you have the following URL:
http://www.dotentboss.com?author=ahsan
and you want to get the "author" querystring's value:
var author_value = getQuerystring('author');
No comments:
Post a Comment