Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Try alerting v to see if you're getting the expected value.

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

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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. "

  • #4
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    Last edited by jamescover; 07-15-2004 at 05:27 PM.
    "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. "

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Vancouver, WA
    Posts
    233
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Regular Coder
    Join Date
    Aug 2002
    Location
    USA
    Posts
    625
    Thanks
    0
    Thanked 0 Times in 0 Posts

    <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. "


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •