shosei

get URL Variables

Mar 11th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Read a page's GET URL variables and return them as an associative array.Read a page's GET URL variables and return them as an associative array.
  2. // Example for URL http://www.example.com/index.html?hello=bonjour&goodevening=bonsoir
  3. // var hash = getUrlVars(); alert(hash['hello']); // prints 'bonjour' alert(hash['goodevening']); // prints 'bonsoir'
  4.  
  5. function getUrlVars()
  6. {
  7.     var vars = [], hash;
  8.     var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  9.  
  10.     for(var i = 0; i < hashes.length; i++)
  11.     {
  12.         hash = hashes[i].split('=');
  13.         vars.push(hash[0]);
  14.         vars[hash[0]] = hash[1];
  15.     }
  16.  
  17.     return vars;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment