document.images when prefetching images

A

Amir

When prefetching images, why do I need the "if (document.images)" statement?

if (document.images) {
pic1on= new Image(300,300);
pic1on.src="pic1.gif";
pic2on= new Image();
pic2on.src="pic2.gif";
picNon= new Image();
picNon.src="pic4.gif";
}
 
K

kaeli

When prefetching images, why do I need the "if (document.images)" statement?

Not all browsers support document.images (object). Trying to access it
would blow up unsupporting browsers (IIRC, Lynx has no such construct
nor do very old versions of netscape). The test returns true only for
browsers that have the object.
Basically, this is referred to as degrading gracefully (that is, the
code won't work "right" for unsupporting browsers, but it won't kill
them with errors, either).
All your code should have such constructs if you're coding for the
internet. You just don't know what browsers people are using. New
browsers come out frequently enough that browser detection is a horrid
idea (plus there's a lot you've probably never heard of). So, object
detection, which is what this is, is the better way to go.

--
--
~kaeli~
It was recently discovered that research causes cancer in
rats.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
I

Ivo

"William Morris" topposted
So that browsers that don't support "document.images" won't blow chunks.

"Amir" asked

You don't. What you want is:
if(window.Image){...}
to prevent errors in browsers that don't understand what you mean by "new
Image()". Since practically all browsers today have no problem with the
Image object (nor with the only slightly related document.images collection)
you might as well drop this feature test altogether.
HTH
Ivo
 
T

Thomas 'PointedEars' Lahn

kaeli said:
(e-mail address removed) enlightened us with...

Please do not write attribution novels, see
Not all browsers support document.images (object). Trying to access it
would blow up unsupporting browsers (IIRC, Lynx has no such construct

Lynx supports J(ava)Script?
nor do very old versions of netscape). The test returns true only for
browsers that have the object. [...]

ACK


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top