Get Value of URL Parameters using JavaScript

Posted by 4 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

Comments (4)

Trackback URL | Comments RSS Feed

  1. Anonymous says:


  2. Azarudheen says:


  3. FirstCamilla says:

    I see you don’t monetize your page, don’t waste your traffic, you can earn extra bucks every
    month because you’ve got hi quality content. If you want to know
    how to make extra $$$, search for: Mrdalekjd methods for

Leave a Reply