Skip navigation.

miscoded

the web is a hack

Posts tagged with "browsers"

odd IE quirk with IMG id attribute

, ,

IE often creates convenience variables for elements with an ID attribute. For example, if your document contains a

<div id="navigation">

your script can refer to "navigation" without calling getElementById. This is also called polluting the global namespace with element references. It is a non-standard feature and your scripts should use getElementById instead of referring to elements directly with convenience variables.

As this testpage shows, IE will also let you refer to an image element with document.imageid - but only if the IMG tag also has a name attribute! And it does not create global variables for IMG elements with IDs!

That is rather inconsistent..

As we see from that test page, Opera is trying too hard to be IE-compatible, it is supporting more window.id references than IE itself. However, Opera isn't aware of the document.imgID approach - which is why this page fails in Opera.

That webmaster could have used a number of syntaxes that would work -
document.images.imgName
window.imgID
window.imgName
document.imgName
document.getElementById('imgID')

and of those three out of five are supported just to cater for lazy webmasters. Unfortunately, the author happened to pick just about the only option Opera had forgotten to implement. Oh well. Life isn't fair, neither is the web.