Connecting Tech Pros Worldwide Forums | Help | Site Map

document.images when prefetching images

Amir
Guest
 
Posts: n/a
#1: Jul 23 '05
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";
}



William Morris
Guest
 
Posts: n/a
#2: Jul 23 '05

re: document.images when prefetching images


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

"Amir" <somewhere@verizon.net> wrote in message
news:ctrtc.26298$yc4.16814@nwrdny02.gnilink.net...[color=blue]
> When prefetching images, why do I need the "if (document.images)"[/color]
statement?[color=blue]
>
> 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";
> }
>
>[/color]


Ivo
Guest
 
Posts: n/a
#3: Jul 23 '05

re: document.images when prefetching images



"William Morris" topposted[color=blue]
> So that browsers that don't support "document.images" won't blow chunks.
>
> "Amir" asked[color=green]
> > 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";
> > }[/color][/color]

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


kaeli
Guest
 
Posts: n/a
#4: Jul 23 '05

re: document.images when prefetching images


In article <ctrtc.26298$yc4.16814@nwrdny02.gnilink.net>,
somewhere@verizon.net enlightened us with...[color=blue]
> When prefetching images, why do I need the "if (document.images)" statement?
>
>[/color]

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

Lee
Guest
 
Posts: n/a
#5: Jul 23 '05

re: document.images when prefetching images


William Morris said:[color=blue]
>
>So that browsers that don't support "document.images" won't blow chunks.[/color]

Too late. They already do.

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#6: Jul 23 '05

re: document.images when prefetching images


kaeli wrote:
[color=blue]
> In article <ctrtc.26298$yc4.16814@nwrdny02.gnilink.net>,
> somewhere@verizon.net enlightened us with...[/color]

Please do not write attribution novels, see
<http://netmeister.org/news/learn2quote.html>
[color=blue][color=green]
>> When prefetching images, why do I need the "if (document.images)" statement?[/color]
>
> Not all browsers support document.images (object). Trying to access it
> would blow up unsupporting browsers (IIRC, Lynx has no such construct[/color]

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

ACK


PointedEars
Closed Thread


Similar JavaScript / Ajax / DHTML bytes