Email conversation
From | David Matthews |
To | Me |
Subject | A mistake and thanks |
Date | 2 October 2007 13:12 |
Hello, whilst working through your really good tutorial, I noticed an
unitialised variable.
http://www.howtocreate.co.uk/tutorials/javascript/dhtml
You declare the variable theElement, the following IF does not cover both
states so later on it will be unitialised for !theElement. I propose var
theElement = 0;
var theElement;
if( document.getElementById ) {
//DOM
theElement = document.getElementById( elementId );
} else if( document.all ) {
//Proprietary DOM
theElement = document.all[ elementId ];
}
if( !theElement ) {
/* The page has not loaded, or the browser claims to
support document.getElementById or document.all but
cannot actually use either */
return;
}
David
From | Me |
To | David Matthews |
Subject | Re: A mistake and thanks |
Date | 2 October 2007 21:01 |
David
> You declare the variable theElement, the following IF does not cover both
> states so later on it will be unitialised for !theElement.
That is by design. It is meant to be unitialised. Assigning it a value of 0
is not needed, because the JavaScript engine will assign it a value of
undefined. When the if statement checks for its value, it will retrieve the
value undefined, which equates to false when used as part of a comparator
(in the same way as 0 also equates to false).
Mark 'Tarquin' Wilton-Jones - author of http://www.howtocreate.co.uk/