Monday, February 4, 2013

Get Query String Parameter with JScript

In order to get client side query string parameter value, you can use this jscript function.

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];
}


Usage:

var paramValue = getQuerystring('keytofindvalue');

You can optionally pass a default value to this function. In this case, if non existing parameter is founded in the query string, the function will return default_ value.

For more detail on this function, please take a look here:
http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx

No comments: