Try the following. It will check to see if the element exists before executing 
the alternateRows() function.

addLoadEvent(function() {
        if(document.getElementById("caseStudiesTable")) {
                alternateRows("caseStudiesTable",6,colors);
        }
});

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Paul Collins
Sent: Thursday, June 21, 2007 1:57 PM
To: [email protected]
Subject: [WSG] Javascript problem

Hi all,

I hope this is on topic, please ignore it if not, I have a small
Jscript problem that shouldn't be hard to sort out, but I am not great
with these things...

I have a script that adds colours to a all the columns in  a table. It
works fine, the only problem is, it is trying to apply the code to all
pages, when the table is only on a couple. So when I am viewing all
other pages, it comes up with this error:

document.getElementById(tableID) has no properties

So, what I would like to do, is add a checker to the script to see if
the table actually exists before doing the rest of the code.
Unfortunately, I am a novice to this and I've been stuffing around for
a while and can't get it to work.

Here is the teh script, it is worth mentioning that this is the only
table on the site, so that may help with the re-working of the code,
although it would be nice to have a checker that looks for the
specific table id.

Thanks in advance:

// script to add alternating table background colours
var colors=["#E5D9DB","#C5D3D8","#DBCBBE","#E9DBC7","#D4E0E0","#C5CEC7"];
        function alternateRows(tableID,numberOfColors,colorArray){
                var 
trs=document.getElementById(tableID).getElementsByTagName("TD");
                len=trs.length;
                var myColors=colorArray.slice(0,numberOfColors);
                while(len--){
                        
trs[len].style.backgroundColor=colors[len%myColors.length];
                        }
                }

// add onload event
addLoadEvent(function() {
        alternateRows("caseStudiesTable",6,colors);
        }
);


*******************************************************************
List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
Help: [EMAIL PROTECTED]
*******************************************************************


******************************************************************************
NOTICE:  The information contained in this message is intended for the 
addressess(s) only and may be confidential, proprietary, or legally
privileged.  If you have received this message in error or there are any
problems with the transmission, please immediately notify us by return
e-mail.  The unauthorized use, disclosure, copying, or alteration of this
message is strictly forbidden.  The sender will not be liable for any 
damages arising from alteration of the contents of this message by a 
third-party or as a result of any virus being transmitted.  This notice
is automatically appended to each e-mail message transmitted from the
sender's e-mail domain.


*******************************************************************
List Guidelines: http://webstandardsgroup.org/mail/guidelines.cfm
Unsubscribe: http://webstandardsgroup.org/join/unsubscribe.cfm
Help: [EMAIL PROTECTED]
*******************************************************************

Reply via email to