jQuery Code Snippet: get URL parameters

jQuery Code Snippet: get URL parameters

jQuery Code Snippet: get URL parameters with jQuery

A feature you need everyday when writing webapplications and RIA is the use of URL parameters. Therefore today I present you a conversion that has been done from the Javascript functionality into the more readable jQuery syntax.I use it in every project, and I hope this is useful for you as being a developer or entrepeneur as well

$.extend(
    {
       
    getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(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];
  }
});

Usage

 //retrieve single param with name "company"
$.getUrlVar("company");
//retrieve an array of all the URL params
var parameters = $.getUrlVars();

About the Author

Lode is a Creative-Geek with passion for webdesign, SEO and everything related to online marketing. In January 2014, he has started his own company Magnefk, which specialises in online marketing and website optimization.