Posted By

a1ias on 07/28/11


Tagged

url


Versions (?)

Get URL parameters using Javascript


 / Published in: JavaScript
 

Usage: Let's say we have an url: http://primera.vremenno.net?foo=yes&bar=no

var hash = getUrlVars(); alert(hash['foo']); // returns 'yes' alert(hash['bar']); // returns 'no'

  1. function getUrlVars(){
  2. var vars = [], hash;
  3. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  4. for(var i = 0; i < hashes.length; i++){
  5. hash = hashes[i].split('=');
  6. vars.push(hash[0]);
  7. vars[hash[0]] = hash[1];
  8. }
  9. return vars;
  10. }

Report this snippet  

You need to login to post a comment.