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 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Exclamation Need Help getting Javascript function to show up!

    Hello I have Created a JavaScript function that is a sort of countdown timer
    and its in an extermal javascript file (.js) and I have tried
    <body onload="janCountDown()"><div id="Jan"></div>
    and it still did not work even with the
    document.getElementById("Jan").innerHTML=t;
    I an new to javascript and that might be my problem but I would like to have some one help me fix it. Here's my function code
    function janCountDown()
    {
    var today = new Date()
    var dayofweek = today.toLocalDateString()
    dayLocate = dayofweek.indexOf(" ")
    weekDay = dayofweek.substring(0, dayLocate)
    newDay = dayofweek.substring(dayLocate)
    dateLocate = newDay.indexOf(",")
    monthLocate = newDay.substring(0, dateLocate+1)
    yearLocate = dayofweek.indexOf(", 2")
    year = dayofweek.substr(yearLocate+2, 4)

    var janNewYear = new Date("January 1, 2013")
    var daysToGo = janNewYear.getTime()-today.getTime()
    var daysToJanNewYear = Math.ceil(daysToGo/(1000*60*60*24))

    document.getElementById("Jan").innerHTML=t;
    displayCountDown.innerHTML = "<p>Today is "+weekDay+" "+monthDate+" "+year+". We have "+daysToJanNewYear+" days until New Years Day.</p>"
    }

  • #2
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    t is not defined anywhere, nor is displayCountDown or monthDate.

    Each of your (presumably local) variables should be preceded with var and JS statements end with semi-colons ;
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • Users who have thanked AndrewGSW for this post:

    iLagPro (12-16-2012)

  • #3
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok so I have fixed (or I think its fixed) but it still is not showing up it acks like its going to but then nothing happens.
    function janCountDown()
    {
    var today = new Date();
    var dayofweek = today.toLocalDateString();
    dayLocate = dayofweek.indexOf(" ");
    weekDay = dayofweek.substring(0, dayLocate);
    newDay = dayofweek.substring(dayLocate);
    dateLocate = newDay.indexOf(",");
    monthLocate = newDay.substring(0, dateLocate+1);
    yearLocate = dayofweek.indexOf(", 2");
    year = dayofweek.substr(yearLocate+2, 4);

    var janNewYear = new Date("January 1, 2013");
    var daysToGo = janNewYear.getTime()-today.getTime();
    var daysToJanNewYear = Math.ceil(daysToGo/(1000*60*60*24));

    document.getElementById("Jan").innerHTML="<p>Today is "+weekDay+" "+monthDate+" "+year+". We have "+daysToJanNewYear+" days to go.</p>";
    }

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,479
    Thanks
    0
    Thanked 635 Times in 625 Posts
    What does the error console say? That's the best place to start when a script doesn't work properly. If that doesn't help then the debugger is the next place to start looking - you can see allt he values at the point it stopped there. (If you are testing on Firefox you will need to install a debugger extension if you haven't already - all the other browsers have one built in).
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    You still don't have a variable named monthDate, which appears in the final string statement.

    As felgall advises, you need to make use of a browser's console.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    That is a strange way to extract the components of a Javascript date object.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by Philip M View Post
    That is a strange way to extract the components of a Javascript date object.
    I assumed the OP is just experimenting..
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #8
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    What does the error console say? That's the best place to start when a script doesn't work properly. If that doesn't help then the debugger is the next place to start looking - you can see allt he values at the point it stopped there. (If you are testing on Firefox you will need to install a debugger extension if you haven't already - all the other browsers have one built in).
    How would I get to the debugger in IE. Because I don't know how to get to the console.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,733
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by iLagPro View Post
    How would I get to the debugger in IE. Because I don't know how to get to the console.
    Press F12

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Quote Originally Posted by iLagPro View Post
    How would I get to the debugger in IE. Because I don't know how to get to the console.
    Documentation here.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS


  •  

    Posting Permissions

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