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 12-15-2012, 11:34 PM   PM User | #1
iLagPro
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
iLagPro is an unknown quantity at this point
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
Quote:
<body onload="janCountDown()"><div id="Jan"></div>
and it still did not work even with the
Quote:
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
Quote:
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>"
}
iLagPro is offline   Reply With Quote
Old 12-16-2012, 12:18 AM   PM User | #2
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Users who have thanked AndrewGSW for this post:
iLagPro (12-16-2012)
Old 12-16-2012, 02:06 AM   PM User | #3
iLagPro
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
iLagPro is an unknown quantity at this point
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.
Quote:
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>";
}
iLagPro is offline   Reply With Quote
Old 12-16-2012, 02:37 AM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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/
felgall is offline   Reply With Quote
Old 12-16-2012, 03:01 AM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 12-16-2012, 10:15 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 12-16-2012, 11:24 AM   PM User | #7
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW is offline   Reply With Quote
Old 12-16-2012, 04:38 PM   PM User | #8
iLagPro
New to the CF scene

 
Join Date: Dec 2012
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
iLagPro is an unknown quantity at this point
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.
iLagPro is offline   Reply With Quote
Old 12-16-2012, 05:02 PM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 12-16-2012, 06:30 PM   PM User | #10
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
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
AndrewGSW 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 10:15 AM.


Advertisement
Log in to turn off these ads.