Get Value of URL Parameters using JavaScript

Posted by 2 Comments

If you are looking for a quick and easy way to grab the value of a query string parameter from a URL, you only need a few lines of JavaScript. The below script will return the value of the matching parameter or return false if no match was found.

function getParameter(theParameter) { 
  var params ='&');
  for (var i = 0; i < params.length; i++) {
    var p=params[i].split('=');
	if (p[0] == theParameter) {
	  return decodeURIComponent(p[1]);
  return false;

The decodeURIComponent() function ensures that any values such as are returned correctly:

So using the example:

we get the following results:

getParameter('test1') --> yes
getParameter('test2') --> no
getParameter('test3') -->
getParameter('test4') --> false

Note that this a very basic script intended as an example to get you started. It provides no validation outside of returning false if no matching parameter was found.

Category: Development, JavaScript, Web Development

Leave a Reply