Get URL parameters using Javascript


/ Published in: JavaScript
Save to your folder(s)

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'


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.