On 6/21/07, Paul Collins <[EMAIL PROTECTED]> wrote:
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'm assuming from the subject that you're actually referring to
Javascript. Jscript is a similar implementation by Microsoft that's
just different enough to screw things up.

So when I am viewing all other pages, it comes up with this error:

document.getElementById(tableID) has no properties

Since there isn't an element with an id attribute equal to tableID,
getElementById returns null.  However, in your script:

                var 
trs=document.getElementById(tableID).getElementsByTagName("TD");

... you're using the method getElementsByTagName of the element object
returned by getElementById.  This is fine, as long as getElementById
returns something, as it does when the table is present on the page.
However, when the table isn't there, getElementById returns null ...
and null doesn't have any properties (or methods), hence the error.
You're basically saying:

var trs = null.getElementsByTagName("TD")

However, it's a simple fix:  run getElementById on its own, check that
it returned something, then run getElementsByTagName on the element
which you now know exists.  Something like so:

function alternateRows(tableID,numberOfColors,colorArray){
 var table = document.getElementById(tableID);
 if (table) {
   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];
   }
 }
}

The stuff inside the if statement won't execute unless table exists.
Hope that helps!

Dan Dorman


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

Reply via email to