Wednesday 16 May 2012

Get QueryStrings using Javascript

function getQuerystring(key, default_) { 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]; }
The getQuerystring function is simple to use. Let's say you have the following URL:

http://www.test.com?a=gopal
and you want to get the "a" querystring's value:
var author_value = getQuerystring('a');

No comments:

Post a Comment