Use variables passed in the URL in Javascript/jQuery

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

(I did not write this, but I forgot where I got it.)

This code will find all the variables passed though the URL and return the one you are looking for.

Copy this code and paste it in your HTML
  1. // This function looks for all vars sent through the address bar
  2. function getUrlVars()
  3. {
  4. var vars = [], hash;
  5. var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
  6. for(var i = 0; i < hashes.length; i++)
  7. {
  8. hash = hashes[i].split('=');
  9. vars.push(hash[0]);
  10. vars[hash[0]] = hash[1];
  11. }
  12. return vars;
  13. }
  15. // This looks to see if 'sent' was a sent through the address bar and saves the value of 'sent' to a new varable called 'NewVar'
  16. var NewVar = getUrlVars()["sent"];

Report this snippet


RSS Icon Subscribe to comments

You need to login to post a comment.