Get URL parameters & values with jQuery

http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html

// Read a page's GET URL variables and return them as an associative array.
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;
}

http://www.example.com/?me=myValue&name2=SomeOtherValue

var
second = getUrlVars()["name2"];


$.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 nam
var byName = $.getUrlVar('name');

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s