473,385 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with Image.onload event firing too early in IE

I'm trying to come up with a solution for detecting when an image,
loaded by a script, has completely loaded. The Image.onload event is
fired after the image has loaded in Firefox but before loading has
completed in Internet Explorer. The function where I assign the
onload event handler looks like this:

var newImage = new Image();

function getImage(url) {
var image = document.getElementById("theImg");
newImage.onload = swapImage;
newImage.src = url;
}

As an attempt at a work-around, in the event handler function
swapImage() I have the following code:

function swapImage() {
var image = document.getElementById("theMap");
if(newImage.complete) {
image.src = newImage.src;
} else {
setTimeout("swapImage()", 250);
}
}

Unfortunately the new image's 'complete' property is set to true prior
to the image fully loading leaving me no better off.

I wondered if this behaviour might be related to the server not
providing the Content-length response header but the headers for the
image seem to be in order:

HTTP/1.x 200 OK
Server: Apache-Coyote/1.1
X-Cocoon-Version: 2.1.6
Vary: Host
Last-Modified: Tue, 25 Jan 2005 11:46:27 GMT
Accept-Ranges: bytes
Content-Type: image/jpeg
Content-Length: 168667
Date: Tue, 25 Jan 2005 11:45:27 GMT

I've considered setting a fixed delay in the event handler if the
browser is IE but I'm not really happy with that approach as the size
of the image to be loaded can be quite variable. The user's internet
connection speed is also unknown.

I'd be very interested in hearing from anybody who has a resolution to
this problem.

Kind regards,
Adam
Jul 23 '05 #1
1 10170
I've resolved this issue. The problem was not as I thought, related to
the behaviour of the onload event, but rather to a failure of IE to
cache the preloaded image. This meant that when the original image's
src attribute was modified to that of the new image there was no image
to load from the cache so a new request was made to the server.

The reason the image was not cached is the 'Vary' response header sent
by the application server (Apache Cocoon). This header is a directive
specifying which request headers are to be used in addition to the
request URI in determining if a suitable match exists in the cache. IE
will _not_ cache responses containing the Vary header unless the field
specified is the "User-Agent" field. For more detail on this issue
see: http://lists.over.net/pipermail/mod_...er/006826.html

Removing the 'Vary' header results in the preloaded images being cached
correctly and the code works as expected.

Cheers
Adam

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Bender | last post by:
Hi, I am wanting to capture an onmousedown event without firing the body tags onload event. Also, if anyone could explain why this happens that would be excellent. I can't see how an...
4
by: joebob | last post by:
The following script if run in Internet Explorer should display a thumbview of a webpage that you point it to. To test it, replace test.htm with a valid html file. The problem I'm having is that...
1
by: khothikhe | last post by:
Can someone help me here because this is really killing me! the problem is, i have this javascript code in my aspx page that i want it to work on almost all browsers, especially opera and safari...
5
by: frizzle | last post by:
Hi there, ijust a quicky: I was wondering if the following is supported everywhere: <img src="spacer.gif" width="1" height="1" onLoad="doomething()"> That way i can launch/check a few things...
6
by: ruca | last post by:
Hi gurus, I have a imagebutton in my WebForm, and I want that when I click (mouse down) on her the imagebutton change image and when I "unclick" (mouse up) change to the original image. Basically...
2
by: Martin Honnen | last post by:
I was playing around with canvas support in recent Safari, Mozilla and Opera (only version 9 preview) but run into issues with Safari related to the very old DOM Level 0 Image object for preloading...
7
by: raknin | last post by:
Hi I have a carousel script. I want to load the carousel with a new set of pictures every time I press a button. The problem that I have that the script append the new pictures to the olds one...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.