Click to See Complete Forum and Search --> : Variable inside the Javascript src=""
keanu122
06-11-2004, 03:51 PM
I'm using a javascript that was generated by weather.com so that I can display the current local weather on my site. The part of the script that sends the zip code is in the src="" definition:
<script src='http://voap.weather.com/weather/oap/ZIPCODE?template=GENXH&par=1005109003&unit=0&key=e705d991c0cd2ef692227ea060aa5025'></script>
Here's the problem...I want the user to customize it so that their zipcode is stored as a variable. So, is it possible to set the variable in one script and then use it in the src definition of another script? I'm thinking that I have run into a limitation of javascript. Any help with implementing a variable in the src definition would be appreciated.
David Harrison
06-11-2004, 07:45 PM
Give this a whirl:<script type="text/javascript"><!--
user_postcode="A12 3BC"
var newscript=document.createElement("script");
newscript.type="text/javascript";
newscript.src="blah.js?zip="+user_postcode;
document.getElementsByTagName("head")[0].appendChild(newscript);
//--></script>I think you'll have to put it into the body section. That shouldn't be a problem though, just put it straight after the opening body tag.
keanu122
06-12-2004, 06:10 PM
Thankyou for your suggested code. Heres what the (kinda) working code looks like:
<script type="text/javascript"><!--
var zcode=prompt("Please Enter your postal code:", "");
var zipsource='http://blah.weather.com/weather'+zcode+'?template=GENXH';
var newscript=document.createElement("script");
newscript.src=zipsource;
document.getElementsByTagName("body")[0].appendChild(newscript);
//--></script>
Good news: I made the newscript.src equal a variable for it to work. The code can be in the head or the body and the newscript can be appended to the head or body also...works the same both ways.
Bad news: The code only works after I enter the zip code in the prompt, hit enter, reload it, and enter the code in the prompt again. Thus, it only seems to work the second time. Has anyone run into a similar problem or can see the problem in the code? Any further help would be appreciated.
David Harrison
06-12-2004, 09:43 PM
Take a look at this example. I have appended the script to the head section because it will then load before the stuff in the body. Make sure that it is placed as the first thing in the body tag as well.
As you can see the script does work because it alerts what is in the .js file. By the way you should leave the text/javascript in, that tells the browser what language you are using.
keanu122
06-12-2004, 11:37 PM
Yea, you're definitely right. Thankyou for the test page you attached; it helped me to clarify/exact my code...but unfortunately, i'm still running into the same problem where it only works some of the time...so i'm going to attach exactly what I have right now so you can see whats going on if you want to. I realize that for learning purposes, we have conquered the problem.
By the way, I have only gotten it to work in preview mode (IE 6.0) of dreamweaver. Never has it actually worked other than as a temp file. Very strange...