Link to home
Start Free TrialLog in
Avatar of Samuraiken
Samuraiken

asked on

Javascript in C# for WSS 3.0 Web Part...how 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("<html>");
            writer.Write("<script language='JavaScript'>");
            writer.Write("<!--");
            writer.Write("var clockID = 0;");
            writer.Write("function UpdateClock() {");
            writer.Write("if(clockID) {");
            writer.Write("clearTimeout(clockID);");
            writer.Write("clockID  = 0;");
            writer.Write("}");
            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("}");
            writer.Write("function StartClock() {");
            writer.Write("clockID = setTimeout('UpdateClock()', 500);");
            writer.Write("}");
            writer.Write("function KillClock() {");
            writer.Write("if(clockID) {");
            writer.Write("clearTimeout(clockID);");
            writer.Write("clockID  = 0;");
            writer.Write("}");
            writer.Write("}");
            writer.Write("</script>");
// 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>");
            writer.Write("<form></center>");
        }
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Samuraiken
Samuraiken

ASKER

Roonaan,
   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?
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 really...it'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!