Get URL Parameters With JavaScript

5 (100%) 1 vote

Get URL Parameters With JavaScript

The JavaScript function below parses and returns the parameters.

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

Then is how you can pick a value from the url parameter.

var number = getUrlVars()["x"];
var mytext = getUrlVars()["text"];


The Complete jf file would like as:

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

var number = getUrlVars()["x"];
var mytext = getUrlVars()["text"];


 

For any query or issue, feel free to discuss on http://discuss.eduguru.in
%d bloggers like this: