www.webdeveloper.com
Results 1 to 8 of 8

Thread: js variable keeps coming up 'undefined'

  1. #1
    Join Date
    Oct 2008
    Location
    moreno valley, California ,usa
    Posts
    161

    js variable keeps coming up 'undefined'

    Greetings colleagues,
    I've boiled down my code to the essence of the problem; a variable which serves as a parameter to a function keeps coming up as 'undefined'. The code is quite simple:
    ....
    <head>
    <title>Yearly Calendar</title>
    <link href="styles.css" rel="stylesheet" type="text/css" />
    <link href="yearly.css" rel="stylesheet" type="text/css" />

    <script type="text/javascript">

    function writeMonthCell(calandarDay, currentTime)
    {
    alert(typeof calendarDay);
    var this_DATE=new Date(calendarDay);
    document.write("inside writeMonthCell the date is: " + this_DATE);

    }//end writeMonthCell function
    </script>
    </head>

    <
    body>


    <div id="main">
    <h1>Yearly Calendar</h1>
    <script type="text/javascript">
    var thisDate=new Date("March 25, 2011 14:35:05");

    var thistime=thisDate.getTime();

    document.write("the date is:" + thisDate+"<br />");
    document.write("the time is:" + thistime+"<br />");

    alert(typeof thisDate);
    writeMonthCell(thisDate, thistime);

    </script>
    </div>
    </body>
    </html>

    in the main body the alert(typeof thisDate) displays 'object'; but when the function is called, writeMonthCell, inside the function the alert says 'calendar undefined' .

    I'm perplexed; why is thisDate now undefined as a parameter?

    Your comments would be very appreciated!!

    captsig

  2. #2
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by captsig View Post
    I'm perplexed; why is thisDate now undefined as a parameter?

    Your comments would be very appreciated!!
    It isn't undefined, you're alerting an undefined variable and if I saw it so can you.

  3. #3
    Join Date
    Aug 2010
    Location
    Baltimore, Maryland, United States
    Posts
    138
    Code:
    function writeMonthCell(calandarDay, currentTime)
    {
    alert(typeof calendarDay);
    var this_DATE=new Date(calendarDay);
    document.write("inside writeMonthCell the date is: " + this_DATE);
    
    }//end writeMonthCell function
    </script>
    How much of human life is lost in waiting?

  4. #4
    Join Date
    Oct 2008
    Location
    moreno valley, California ,usa
    Posts
    161
    if I take out the line for the alert(typeof calendarDay), then the script still produces an error for the next line saying "calendarDay undefined"

  5. #5
    Join Date
    Jun 2007
    Posts
    667
    Quote Originally Posted by captsig View Post
    if I take out the line for the alert(typeof calendarDay), then the script still produces an error for the next line saying "calendarDay undefined"
    "You see but you do not observe" - 'Sherlock Holmes' A Scandal in Bohemia

  6. #6
    Join Date
    Oct 2008
    Location
    moreno valley, California ,usa
    Posts
    161
    I finally have things moving ahead again; thanks for your consideration just the same.
    In the line:function writeMonthCell(calandarDay, currentTime), you normally think of calendarDay and currentTime as a place-holding parameters; I guess this is not so for date objects. Once I made sure that the calling statement had the same param. name, i.e. calendarDay the errors for 'undefined' stopped appearing.

    captsig

  7. #7
    Join Date
    Aug 2010
    Location
    Baltimore, Maryland, United States
    Posts
    138
    You spelled calendar "calandar" in the function declaration. That was the only problem.
    How much of human life is lost in waiting?

  8. #8
    Join Date
    Oct 2008
    Location
    moreno valley, California ,usa
    Posts
    161
    Ofekmeiter!! you're right!! How stupid of me. Amazing how the obvious can escape us.

    Thanks for your response.
    captsig

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles