Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

broken javascript in included file

Status
Not open for further replies.

janinja

Technical User
Nov 28, 2000
18
NZ
Trying to implement a javascript image clock which sits in an included file to be used throughout my website.

Have mapped all the images with virtual paths, and it works fine when I test the included file on it's own.

However, when I browse the file as an include in another page, it doesn't work. The images are being called correctly (the default starting images are displaying) and I'm not getting any error messages. But the clock fails to increment.

I have a hunch that it might be something to do with the body onload syntax because it only doesn't work as an included page and also just doesn't seem to start executing.

I will be very grateful to anyone who has an idea of what's wrong.

janinja : )
 
looks like you're missing something in including the file. But if it was the case, you would have had an error ... are you sure the browser is not saying something like "missing object in line 1 " or something ??? there is no jscript error at all ???? then send the body onload code ?
 
This is the javascript in the head

<script language=&quot;JavaScript&quot;>
<!--

var timerID = null;
var timerRunning = false;

function stopclock(){
if(timerRunning) clearTimeout(timerID); timerRunning = false
}

function startclock(){
stopclock(); showtime()
}

function showtime(){
var now = new Date();
var hours = now.getHours();
var minutes = now.getMinutes();
var fig1time = parseInt(hours / 10);
var fig2time = hours - (fig1time * 10);
var fig3time = parseInt(minutes / 10);
var fig4time = minutes - (fig3time * 10);

if (hours < 10) var fig1time = 0;
if (hours < 10) var fig2time = hours;
if (minutes < 10) var fig3time = 0;
if (minutes < 10) var fig4time = minutes;
if (hours==0) hours=12;
document.hour1.src = &quot;/engine/templates/clock_images/&quot; + fig1time + &quot;.gif&quot;;
document.hour2.src = &quot;/engine/templates/clock_images/&quot; + fig2time + &quot;.gif&quot;;
document.minute1.src = &quot;/engine/templates/clock_images/&quot; + fig3time + &quot;.gif&quot;;
document.minute2.src = &quot;/engine/templates/clock_images/&quot; + fig4time + &quot;.gif&quot;;
timerID = setTimeout(&quot;showtime()&quot;,1000);
timerRunning = true}

//--></script>

this is the body onload handler:

<body onload=&quot;startclock()&quot;>

it works just as a normal javascript should do - only not as part of an included page!
 
oh - forgot to mention - definitly is no error message. clock just refuses to increment.
 
janinja

It is difficult to tell what is going wrong, however I would begin debugging by adding a button to the a page that is including the javascript. Make the onClick event hand for the button start the clock if the clock starts the javascript is ok. Also add an alert message to the page onLoad event to confirm that this is being called correctly.

Is it possible that you have created two body tags, you must ensure that the final page source after the include is valid html.

Chris
 
I haven't tried this yet - was on a course yesterday so only just got the message. Thanks Thrud and Iza, I'm sure that will work, but if it doesn't I'll make a custom tag or summat : )
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top