David Matthews

Navigation

Skip navigation.

Search

Site navigation

Email conversation

FromDavid Matthews
ToMe
SubjectA mistake and thanks
Date2 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
FromMe
ToDavid Matthews
SubjectRe: A mistake and thanks
Date2 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/
This site was created by Mark "Tarquin" Wilton-Jones.
Don't click this link unless you want to be banned from our site.