[thelist] DIV, innerHTML, and Mozilla Compatibility

Abdullah Shaikh abdullah at uxdg.com
Sun Feb 16 12:37:00 CST 2003


--
[ Picked text/plain from multipart/alternative ]
use this little script, it will work for DOM (including IE5.x - 6.x) as well as IE4.

    function get(eN){if(document.getElementById){return(eval('document.getElementById(\'' + eN + '\')'))}else{if document.all){return(eval('document.all.' + eN))}}}

so, anytime you need to access an object, just use get('objectName') -- for exaple...

    alert(get('objectName').style.cssText);

You can easily extend it by adding an NS4 branch ( document.layers ) to use NS4 as well for those 1% of slackers that haven't upgraded their browsers yet...

- abdullah
----------------------


----- Original Message -----
From: ".jeff" <jeff at members.evolt.org>
To: <thelist at lists.evolt.org>
Sent: Saturday, February 15, 2003 10:00 AM
Subject: RE: [thelist] DIV, innerHTML, and Mozilla Compatibility


> 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