Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2008-03-05
Medium Priority
Last Modified: 2009-02-04
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

Question by:Samuraiken
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 49

Accepted Solution

Roonaan earned 1500 total points
ID: 21057846
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


Author Comment

ID: 21057990
   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?

Author Comment

ID: 21065870
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!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question