Skip to Content

TheBlogReaders.com

Salesforce.com, PHP, MySQL, Javascript, Ajax, Htacces

Get URL Parameters (QueryStrings) using Javascript

Be First!
by March 23, 2012 JAVASCRIPT
function getQueryStringValue(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 getQueryStringValue function is simple to use. Let’s say you have the following URL:
http://www.themoderneducation.com?YourName=TME
and you want to get the “YourName” querystring’s value:
var YourName_Value = getQueryStringValue(‘YourName’);
alert(YourName_Value); // Result = “TME”

(1092)

Previous
Next

Leave a Reply