...

View Full Version : Variable Not Getting Defined



peterinwa
07-15-2004, 06:33 AM
// 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

glenngv
07-15-2004, 08:25 AM
Try alerting v to see if you're getting the expected value.

v=getCookie("listNumCookie");
alert(v);
...

jamescover
07-15-2004, 08:38 AM
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

jamescover
07-15-2004, 08:55 AM
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

peterinwa
07-15-2004, 06:08 PM
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

jamescover
07-15-2004, 06:26 PM
<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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum