Posts Tagged ‘Querystring Parameters’

This snippet extends the document object to provide dictionary type access to the querystring parameters.

Usage:
// http://www.xxx.com?param1=hello
var paramValue = document.getParameterByName('param1');
// paramValue is now 'hello'

 

document.getParameterByName = function (name) {
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.href);
  if (results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
};
Advertisements