Get URL Variables

PHP Tricks provide a function which helps to get URL parameters using JavaScript. This is very easy function to quick edit.

function getURLVariable(variable)
{
       var query = window.location.search.substring(1);
       var vars = query.split("&");
       for (var i=0;i<vars.length;i++) {
               var pair = vars[i].split("=");
               if(pair[0] == variable){return pair[1];}
       }
       return(false);
}

Usage

Example URL:
http://www.demosite.com/index.php?mid=10&image_name=test.jpg

Calling getURLVariable("mid") – would return “10“.
Calling getURLVariable("image_name") – would return “test.jpg“.

By Ashutosh Pratap

I am software developer by profession and doing work and research in field of Computer programmings like PHP, WordPress, Magento, jQuery, Google APIs and many more web languages. Apart from this I write blogs on programming languages like PHP, Javascript, WordPress etc. I am also individual and independent Freelancer so you can hire me for your web work.

Leave a comment

Your email address will not be published. Required fields are marked *