How to get values from the URL using javascript?

When we are in need to get values from the URL using java script as we get them using $_REQUEST[‘name’]; in PHP, $q->param(‘name’); in Perl etc. we can use the following function to get the values in java script :

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
         vars[key] = value;
    });
    return vars;
}

To get the values we should call the above function as follows :

var val1=getUrlVars()["name1"];
alert(val1);

If the URL is http://ww.example.com/page.php?name1=value1&name2=value2&name3=value3, then the

var val1=getUrlVars()["name1"]; //value1
var val2=getUrlVars()["name2"]; //value2
var val2=getUrlVars()["name2"]; //value3