Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-15-2004, 05:33 AM   PM User | #1
peterinwa
Regular Coder

 
Join Date: Jun 2002
Location: Vancouver, WA
Posts: 233
Thanks: 2
Thanked 0 Times in 0 Posts
peterinwa is an unknown quantity at this point
Variable Not Getting Defined

// Conditionally load .js file
v=getCookie("listNumCookie");
if (v==101){document.write("<" + "script src='list_misc.js'><" + "/script>")} // defines var misc
if (v==102){document.write("<" + "script src='list_dairy.js'><" + "/script>")} // defines var dairy

if (v==101){c=misc}
if (v==102){c=dairy}

alert(c);

The alert is saying undefined. I can't figure out why because I am sure the .js file is being loaded. I say this because I can put some illegal code in the .js file and get an error message about it.

This code in is just one of many Frames, but I don't see what difference that would make as I'm loading the .js file into this frame.

Thanks, Peter
peterinwa is offline   Reply With Quote
Old 07-15-2004, 07:25 AM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Try alerting v to see if you're getting the expected value.

v=getCookie("listNumCookie");
alert(v);
...
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 07-15-2004, 07:38 AM   PM User | #3
jamescover
Regular Coder

 
Join Date: Aug 2002
Location: USA
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
jamescover is an unknown quantity at this point
The alert is saying undefined.


You have to define the variables globally, outside the function, to alert the value without invoking the function....

Where is the rest of your script, the function, variable definitions (misc, dairy), etc.





-james
__________________
"God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "
jamescover is offline   Reply With Quote
Old 07-15-2004, 07:55 AM   PM User | #4
jamescover
Regular Coder

 
Join Date: Aug 2002
Location: USA
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
jamescover is an unknown quantity at this point
Try something like:



<script>
<!--

function someFunc(){

var c = "";
var v = getCookie("listNumCookie");

if (v == 101){
c = "list_misc.js";
alert(c);
} else if (v == 102){
c = "list_dairy.js";
alert(c);
}
document.write('<script src="'+ c +'"><\/script>');
}

//-->
</script>


-james
__________________
"God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "

Last edited by jamescover; 07-15-2004 at 05:27 PM..
jamescover is offline   Reply With Quote
Old 07-15-2004, 05:08 PM   PM User | #5
peterinwa
Regular Coder

 
Join Date: Jun 2002
Location: Vancouver, WA
Posts: 233
Thanks: 2
Thanked 0 Times in 0 Posts
peterinwa is an unknown quantity at this point
Thanks for helping me think it through.

I fixed it by putting everything in the <head> but the calls to the variables which are in the <body>.

Peter

Last edited by peterinwa; 07-15-2004 at 05:24 PM..
peterinwa is offline   Reply With Quote
Old 07-15-2004, 05:26 PM   PM User | #6
jamescover
Regular Coder

 
Join Date: Aug 2002
Location: USA
Posts: 625
Thanks: 0
Thanked 0 Times in 0 Posts
jamescover is an unknown quantity at this point

<script>
<!--

function someFunc(){

var c = "";
var v = getCookie("listNumCookie");

if (v == 101){
c = "list_misc.js";
alert(c);
} else if (v == 102){
c = "list_dairy.js";
alert(c);
}
document.write('<script src="'+ c +'"><\/script>');
}

//-->
</script>



If you are getting "undefined" values for v, then you're cookie data is not being passed. If you change:

var v = getCookie("listNumCookie");

to:

var v = 101;

or

var v = 102;

you will get the proper values for v...



-james
__________________
"God so loved the world that he gave his only begotten son, so that whosoever believed in him would not perish, but have everlasting life. For God did not send his son into the world to condemn the world, but so that through him the world might be saved. "
jamescover is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:56 AM.


Advertisement
Log in to turn off these ads.