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

1man on 03/08/07


Tagged

url query extract


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

basicmagic
vali29


Query Variable Function


Published in: JavaScript 


This function extracts a value from a url. Very useful for tracking codes. For example, you have a url http://your.url/?source=666999, and you need the source number to be placed into a variable. Use the code below.

  1. variable = getQueryVariable("what you want to extract");//e.g "source"
  2.  
  3. function getQueryVariable(variable) {
  4. var query = window.location.search.substring(1);
  5. var vars = query.split("&");
  6. for (var i=0;i<vars.length;i++) {
  7. var pair = vars[i].split("=");
  8. if (pair[0] == variable) {
  9. return pair[1];
  10. }
  11. }
  12. }

Report this snippet 

You need to login to post a comment.