By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,254 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,254 IT Pros & Developers. It's quick & easy.

document.images when prefetching images

P: n/a
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";
}
Jul 23 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
So that browsers that don't support "document.images" won't blow chunks.

"Amir" <so*******@verizon.net> wrote in message
news:ct*******************@nwrdny02.gnilink.net...
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";
}

Jul 23 '05 #2

P: n/a
Ivo

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

"Amir" asked
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";
}


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
Jul 23 '05 #3

P: n/a
In article <ct*******************@nwrdny02.gnilink.net>,
so*******@verizon.net enlightened us with...
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

Jul 23 '05 #4

P: n/a
Lee
William Morris said:

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


Too late. They already do.

Jul 23 '05 #5

P: n/a
kaeli wrote:
In article <ct*******************@nwrdny02.gnilink.net>,
so*******@verizon.net enlightened us with...
Please do not write attribution novels, see
<http://netmeister.org/news/learn2quote.html>
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


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
Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.