get the GET variable of one URL into variable


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



Copy this code and paste it in your HTML
  1. /*
  2.  * Allow to get into associative array, the GET variable of one URL
  3.  *
  4.  * USING:
  5.  * To get all the variable of one URL
  6.  * var a = getUrlVars('http://domaine.tld/index.php?me=beauGosse&Robert=Moche')["me"];
  7.  * return {"me" : "beauGosse", "Robert" : "Moche"}
  8.  *
  9.  * To get a value of first parameter you would do this:
  10.  * var f = getUrlVars('http://domaine.tld/index.php?me=beauGosse&Robert=Moche')["me"];
  11.  * return beauGosse
  12.  *
  13.  * To get the second parameter
  14.  * var s = getUrlVars('http://stan.com/index.php?me=beauGosse&Robert=Moche')["Robert"];
  15.  * return Moche
  16.  *
  17.  * INSPIRATION:
  18.  * http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
  19.  */
  20. function getVarsGet(url)
  21. {
  22. var vars = {}, hash;
  23. var hashes = url.slice(url.indexOf('?') + 1).split('&');
  24. for(var i = 0; i < hashes.length; i++)
  25. {
  26. hash = hashes[i].split('=');
  27. vars[hash[0]] = hash[1];
  28. }
  29. return vars;
  30. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.