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

Preloading Image

P: n/a
Hi. I have one image that has to show up in a html page several times.
To save time, I'd like to preload this image. As an example:

if (document.images) {
tester = new Image()
tester.onload=function() {cImg('good')}
tester.onerror=function() {cImg('bad')}
tester.src="image.jpg"
}
function cImg(typ) {
if (typ == 'bad') {alert('bad image') ; return}
document.images.main.src = tester.src
}
Problem, at least on my local html copy. When my IE6 loads the image,
tester.src changes from "image.jpg" to
"file:///C:/Inetpub/wwwroot/mas/image.jpg". document.images.main.src =
tester.src", therefore, doesn't work. Question. After my onload, if I
did document.images.main.src = "image.jpg", wouldn't another copy of
"image.jpg" have to load?

I was thinking. Couldn't I set the object document.images.main to the
object tester? If so, how?

This may be no big deal, but I'd like my offline copy of my web site to
work like my online copy...Dennis
Jul 23 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 20 Mar 2005 15:26:44 -0500, "Dennis Allen"
<de****@dennisallen.com> wrote:
After my onload, if I
did document.images.main.src = "image.jpg", wouldn't another copy of
"image.jpg" have to load?


No, that isn't how preloading works, it's all about URI's it's got
nothing to do with what you put in the document, if the URI's the same
then the browser _may_ cache it.

Browsers rarely cache file:// resources not surprisingly...

Jim.
Jul 23 '05 #2

P: n/a

To do my good deed as someone gave me a hand with some problem I had:

What you are implying is caching of the image and it depends on the
settings of the browser whether it will cache an image or download it
every single time.

If the settings of the browser lean toward using cached objects rather
than downloading everything, then so long as you give the exactly the
same location of the object, the browser will use the cached object.

Preloading objects is all about getting objects ready for event
processing (i.e. changing an image on a mouse over) where speed is of
the essence. But I suppose you could use it to force caching.

I wouldn't trust testing locally as your browser will sense that you
are local and won't cache!

Paul

On Sun, 20 Mar 2005 15:26:44 -0500, "Dennis Allen"
<de****@dennisallen.com> wrote:
Hi. I have one image that has to show up in a html page several times.
To save time, I'd like to preload this image. As an example:

if (document.images) {
tester = new Image()
tester.onload=function() {cImg('good')}
tester.onerror=function() {cImg('bad')}
tester.src="image.jpg"
}
function cImg(typ) {
if (typ == 'bad') {alert('bad image') ; return}
document.images.main.src = tester.src
}
Problem, at least on my local html copy. When my IE6 loads the image,
tester.src changes from "image.jpg" to
"file:///C:/Inetpub/wwwroot/mas/image.jpg". document.images.main.src =
tester.src", therefore, doesn't work. Question. After my onload, if I
did document.images.main.src = "image.jpg", wouldn't another copy of
"image.jpg" have to load?

I was thinking. Couldn't I set the object document.images.main to the
object tester? If so, how?

This may be no big deal, but I'd like my offline copy of my web site to
work like my online copy...Dennis


Jul 23 '05 #3

P: n/a
"Jim Ley" <ji*@jibbering.com> wrote in message
news:42****************@news.individual.net...
On Sun, 20 Mar 2005 15:26:44 -0500, "Dennis Allen"
<de****@dennisallen.com> wrote:
After my onload, if I
did document.images.main.src = "image.jpg", wouldn't another copy of
"image.jpg" have to load?


No, that isn't how preloading works, it's all about URI's it's got
nothing to do with what you put in the document, if the URI's the same
then the browser _may_ cache it.

Browsers rarely cache file:// resources not surprisingly...


Every preload example I've seen goes src = src. Just checked my old
Windows 98 machine. It has IE6 which also adds "file://" to tester.src.
Is it possible to set the <img> object with the Image() object?
Jul 23 '05 #4

P: n/a
On Mon, 21 Mar 2005 00:01:26 -0500, "Dennis Allen"
<de****@dennisallen.com> wrote:
"Jim Ley" <ji*@jibbering.com> wrote in message
No, that isn't how preloading works, it's all about URI's it's got
nothing to do with what you put in the document, if the URI's the same
then the browser _may_ cache it.

Browsers rarely cache file:// resources not surprisingly...


Every preload example I've seen goes src = src. Just checked my old
Windows 98 machine. It has IE6 which also adds "file://" to tester.src.
Is it possible to set the <img> object with the Image() object?


No, there's no such thing as an image object with any actual image
information in it, and it's completely irrelevant to the ability to
cache it.

I can't think off hand a of a browser that does not expand the url to
the full one in img.src

Jim.

Jul 23 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.