Javascript in C# for WSS 3.0 Web would i call a javascript function?

This is problably extremely easy, however i fail to see where i'm missing something.
I have code for a web part that i built, which reads from a list in sharepoint, and grabs all items to output them into a javascript array.  This works fine.  My main goal, however, is to have a clock running within my web part, that checks the "Start Time" field of each list item and compares it to the current time on the clock.  This would happen once per minute.  However, when i put the code for my clock into the web part, i'm not certain what to do.

For example...

All of my javascript is being output with Writer.Write:

In the code below, this simple javascript clock SHOULD run, but doesn't.  I think the problem lies in the fact that the clock starts by calling the StartClock() function on the HTML body tag's onload property.

Here is the line:
Writer.Write("<body onload='StartClock()' onunload='KillClock()'>");

Is there something else that needs to be done?  I understand that the web part is already on a page, and therefore it would already have a body tag.  But is there another way of calling this function to start the clock?  Below is the full code listing
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Timers;
using System.Web;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
namespace readList
    public class Hello : Microsoft.SharePoint.WebPartPages.WebPart
        protected override void RenderContents(HtmlTextWriter writer)
            writer.Write("<script language='JavaScript'>");
            writer.Write("var clockID = 0;");
            writer.Write("function UpdateClock() {");
            writer.Write("if(clockID) {");
            writer.Write("clockID  = 0;");
            writer.Write("var tDate = new Date();");
            writer.Write("document.theClock.theTime.value = '' ");
            writer.Write("+ tDate.getHours() + ':' ");
            writer.Write("+ tDate.getMinutes() + ':' ");
            writer.Write("+ tDate.getSeconds();");
            writer.Write("clockID = setTimeout('UpdateClock()', 1000);");
            writer.Write("function StartClock() {");
            writer.Write("clockID = setTimeout('UpdateClock()', 500);");
            writer.Write("function KillClock() {");
            writer.Write("if(clockID) {");
            writer.Write("clockID  = 0;");
// This is where i think the problem lies, but i could be very wrong.
// I've been working on this "clock" project for a week now and i just want to cry!  hahaa
            writer.Write("<body onload='StartClock()' onunload='KillClock()'>");
            writer.Write("<center><form name='theClock'>");
            writer.Write("<input type=text name='theTime' size=8>");

Open in new window

Who is Participating?
RoonaanConnect With a Mentor Commented:
I think you should approach this from your webbrowsers view. Do you get a javascript error on Internet Explorer, or do you see notices and/or warnings in the javascript console in firefox?
The code looks ok at a first glance, although I would interchange document.theClock.theTime.value with document.forms['theClock'].theTime.value for better crossbrowser compatability.

For debugging purposes you could add alerts() to startClock and updateClock functions, to see if the functions aren't called, or that there is a problem with just passing the time value to your form.

Kind regards

SamuraikenAuthor Commented:
   I get no errors in either browser, but that could be because this is a web part that i'm building that is loaded onto a page within WSS 3.0.  I'm not sure if that would affect this.

I edited the code to put an alert ("Start Clock") and alert ("Kill Clock") in the StartClock() and KillClock() functions.

I put the javascript into Notepad, saved the file on my desktop as index.html, and opened it up in Firefox.  
This is just a normal page, not within sharepoint.   It worked perfect (i got an alert saying "Start clock" when opening the page, and one saying "Kill clock" when closing the page).

However when i opened it in Internet Explorer, i got the security popup asking if i wanted to enable activeX controls.  After saying "Yes", the page reloaded but nothing was on the page.  So in this case, FireFox displayed it and IE did not.

So then i decided to test this on my WSS 3.0 server.  I installed Firefox, and opened the page in IE and FireFox.  IE did not show anything but a blank web part.  FireFox displayed an Input Box (<input type="text">) control on the page, but with no text actually inside of it.

Any idea why?
SamuraikenAuthor Commented:
I think, rather than doing this in a web part, i'm goinng to just stick with a normal .html file with javascript in it.  The only problem is that i will have to modify it myself (i mean's the end of the world if we think end users are going to modify javascript to change data in an array).  Thanks for your help!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.