We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

Roshambo on 08/14/06


Tagged

get url textmate hash


Versions (?)


Who likes this?

39 people have marked this snippet as a favorite

meth
postNuKe
panatlantica
hxseven
kompo
pablodgavilan
nutella
Hirmine
eunjoo1984
hollowmyth
fael
robotoverlord
vali29
Winkyboy
jfherring
tcol
SpinZ
martinkas
Wizzle
wbowers
lujie
nerdfiles
hans
Leech
rubensarrio
ilumin
jeremyhcobb
bigheadlyf
ira
ikimozu
a1ias
idrumgood
jodm
xmartyxcorex
risico
narfdre
anobre
eivind
masondixon


Get URL Variables


Published in: JavaScript 


Read a page's GET URL variables and return them as an associative array.

Example for URL http://www.example.com/index.html?hello=bonjour&goodevening=bonsoir

var hash = getUrlVars(); alert(hash['hello']); // prints 'bonjour' alert(hash['goodevening']); // prints 'bonsoir'

  1. // Read a page's GET URL variables and return them as an associative array.
  2. function getUrlVars()
  3. {
  4. var vars = [], hash;
  5. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  6.  
  7. for(var i = 0; i < hashes.length; i++)
  8. {
  9. hash = hashes[i].split('=');
  10. vars.push(hash[0]);
  11. vars[hash[0]] = hash[1];
  12. }
  13.  
  14. return vars;
  15. }

Report this snippet 

Comments

RSS Icon Subscribe to comments
Posted By: Uzbekjon on September 16, 2009

Great script. I also adopted it for jQuery as an extension.

So check out Getting URL parameters by name in jQuery code snippet.

Posted By: the_coder on September 16, 2009

Take a look here for a shorter solution: http://snipplr.com/view/19838/get-url-parameters/

You need to login to post a comment.