[thelist] DIV, innerHTML, and Mozilla Compatibility

.jeff jeff at members.evolt.org
Sat Feb 15 12:04:01 CST 2003


nebula,

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> From: Nebula
>
> Hello.  I have recently been working on coding some
> JavaScript that changes the contents of DIV tags.  I
> got it completely working fine in Internet Explorer (I
> use document.all.*divID*.innerHTML to change it in IE).
>
> Now I have recently began using Mozilla, and I really
> like it.  But, when I loaded my Jscript into Mozilla,
> it just choked on it, and did nothing.  The error from
> Mozilla's JavaScript console is as follows:
>
> ----------
> Error: document.all has no properties
> Source File: http://localhost/download.js
> Line: 26
> ----------
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

the document.all collection is an ie-only construct.  the closest thing to
it in mozilla is document.getElementById(), which ie5+ supports,
fortunately.

><><><><><><><><><><><><><><><><><><><><><><><><><><><><><
> eval('document.all.' + divName + '.innerHTML = "' +
> divValue + '";');
><><><><><><><><><><><><><><><><><><><><><><><><><><><><><

first of all, lose the eval() method.  its use makes your code very
difficult to write and read.  worse, it's a performance hog and will kill
the execution time on your script.  you can access properties of the all
collection using bracket notation, like this:

document.all[divName].innerHTML = divValue;

getting back to document.getElementById() though, this is how i'd alter your
function (assuming divName is an id being passed to the function and that id
only exists once in the document):

document.getElementById(divName).innerHTML = divValue;

hope this helps,

.jeff

http://evolt.org/
jeff at members.evolt.org
http://members.evolt.org/jeff/




More information about the thelist mailing list