Looking to Build/Redesign a Website or Blog ?

To speak to Rakshit, call +91 968 704 2303

Get URL variables in javascript/jQuery

Hello Friends,

Yesterday i was in search of javascript/jQuery code for getting URL variables in javascript. Once i will get the URL variables in javascript file i can make further process with it. I came across below code which saves lots of my time.

I came across this blog where i found the solution.

//For Javascript

function getUrlVars()

{

    var vars = [], hash;

    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)

    {

        hash = hashes[i].split('=');

        vars.push(hash[0]);

        vars[hash[0]] = hash[1];

    }

    return vars;

}

//For jQuery

$.extend({

  getUrlVars: function(){

    var vars = [], hash;

    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++)

    {

      hash = hashes[i].split('=');

      vars.push(hash[0]);

      vars[hash[0]] = hash[1];

    }

    return vars;

  },

  getUrlVar: function(name){

    return $.getUrlVars()[name];

  }

});

// Get object of URL parameters

var allVars = $.getUrlVars();

// Getting URL var by its name

var byName = $.getUrlVar('name');

 

If you find any problem in this than let me know by comment.

Share:

1 Comment

  • My Search Engin

    My Search Engin…

    Most relevant results for every search term! and Best Search Engine on the Internet, here you will find everything you want…

Leave a Reply

Your email address will not be published.