Javascript - To get URLs in JS

This function is used to get values from URL.

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

Eg: URL: http://www.xyz.com?type=test
Output:   test

So that we can check through Javascript,
var type = getUrlVars()["type"];
if(type == "test") {
alert(" I am here");
}

Comments

Popular posts from this blog

Ghostscript Installation and Configuration in Windows 7

Joomla - Get user id