[thelist] passing a JS variable to another page? Can I?

Erik Mattheis gozz at gozz.com
Sun May 13 14:45:30 CDT 2001


>I imagine there has to be a way to pull a variable from the
>link/address bar in javascript, right?  so that something like
>"A href='webpage.html?santa'" would be the link, and
>webpage.html could declare a variable that pulls in that item?
>
>My reasoning is I'd like to create one page that I can call
>using different variable to, for instance, show various images.
>
>Is this possible?  I know JS enough to make useful functions and
>to know what I'm reading, I just haven't been able to find
>anything like this yet.


Fun problem ... there might be a more eloquent way of doing it, but 
this seems to work:

if your location is /index.html?chocolate=good&foo=bar

you can say:

query_string = 
unescape(location.href.substring(location.href.indexOf('?') + 1, 
location.href.length));

query_variables = query_string.split('&')

foo = '';
chocolate = '';
for (var i=0; i < query_variables.length; i++) {
   name_value_pair = query_variables[i].split('=');
   if (name_value_pair[0] == 'foo') {
     foo = name_value_pair[1];
   }
   else if (name_value_pair[0] == 'chocolate') {
     chocolate = name_value_pair[1];
  }
}


alert('the value of chocolate is ' + chocolate + '\n and foo is ' + foo);
-- 

- Erik Mattheis

"For best results, pronounce muh THEIGH ess."

(612) 827 3963




More information about the thelist mailing list