Tuesday, 24 July 2012

Get URL values using javascript

The following piece of code will fetch URL variables & Its values.

var first = getUrlVars()["id"];
var second = getUrlVars()["page"];

alert(first);
alert(second);

The Code

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

No comments:

Post a Comment