Forum Moderators: open

Message Too Old, No Replies

not display image until fully loaded

         

merijnvw

10:11 pm on Oct 20, 2009 (gmt 0)

10+ Year Member



I have a page that contains a very big image, and when it loads it first is blurry for a long time in most browsers, then becomes normal. I want to display a text like 'loading... please be patient' while the image is loading and is being hidden. How can I do this? Preferably in Javascript(I'm not so familiar with AJAX).
I know I'd even better use a preload bar but I can't get one to work neither can I find a working one on another site.

rainborick

3:44 pm on Oct 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can do this two ways that I know of - the easy way and the hard way. The easy way is to put some JavaScript in the <head> section of your page to load the image into a dummy variable so that it loads before the rest of the page. Then when the browser tries to render the actual <img> tag, that data is already in the cache so it appears along with the rest of the page. Of course, this doesn't let you do the nifty "loading..." message or other effect, but it has the upside of not displaying the image in stages as it loads in real time. That was the easy way.

The hard way involves adding an "onload" handler on a dummy image variable in some code that's embedded in the page near the actual <img> tag. You initially display a low-res image that says "loading..." or whatever while the onload function waits for an image to be loaded. The tricky part is that images in Internet Explorer do not fire an onload event. You need to use a readyStateChange handler and a lot of event handling code for Internet Explorer (all stuff that I've never worked with so I can't advise you) and use onload for everyone else. I did a brief search last night on "img onload event" and found quite a few references. So with a bit of research, you should be able to find some examples that you can use as the basis for your own code.

Fotiman

5:28 pm on Oct 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



A couple of things to consider.

1. Presumably the big image is your actual content. In general, it is not a good idea to make your content dependent on JavaScript or CSS being present. That is, a user that does not have JavaScript or CSS should still be able to see your content. So if you try to do something like put a small image in the page and then use JavaScript to load the real image and replace the small one, you're making your content inaccessible to some users.

2. If your image loads blurry and then clears up, it means your image is interlaced. Interlaced images allow the user to get an idea of what the image will look like before it has finished loading. Essentially, you're providing them with a glimpse of the content as it's loading. If you show a "loading" image instead, you are actually degrading the user experience because the user now has to wait for the entire image to load before getting any indication what that content will look like. What is the purpose of showing a "loading" image? Does it add any value, and if so, how?

3. The "easy" way that rainborick suggested (putting JavaScript in the <head> that loads a dummy img) has drawbacks as well. It will block other content from being loaded until the image has finished loading, which can give the appearance of an unresponsive website (a blank page).

An alternative might be to keep your html as it is. At the end of your HTML document (just before the closing </body> tag), include a script that creates the same dummy image and displays a "loading" overlay on top of the image (rather than hiding the image), and then hiding the "loading" image when it has loaded. Below is an example (note, I'm using the YAHOO UI Library's DOM Utility for working with the class). You would also need to define the CSS for two classes:


.loadingImage {
/* absolutely position the image on top of your big image */
}
.loadingComplete {
/* loading complete, so hide this */
display: none;
}

Include the YAHOO Dom Utility:

<script type="text/javascript" src="http://yui.yahooapis.com/2.8.0r4/build/yahoo-dom-event/yahoo-dom-event.js"></script>

And include your own script that looks like this:


// Self executing anonymous function
(function () {
var bigImg = new Image(),
elImg = document.createElement('img');
elImg.src = 'loading.png';
// Attach the loading presentation
YAHOO.util.Dom.addClass(elImg, 'loadingImage');
document.appendChild(elImg);
bigImg.onload = function () {
// When this fires, hide the loading image
YAHOO.util.Dom.replaceClass(elImg, 'loadingImage', 'loadingComplete');
};
bigImg.src = 'bigimage.jpg';
})();

Note, I've not heard of any onload handler problems in IE. Let us know if this works. :)

merijnvw

10:06 pm on Oct 22, 2009 (gmt 0)

10+ Year Member



Thanks a lot for your helpfulness rainborick and Fotiman!
The content can be dependent on JavaScript as the whole site uses Javascript. the big image is a huge ancient map called the Carta Marina by Olaus Magnus and I'm making a kind of interactive map viewer. There are frames around it with arrows so the user can move through the map and there is also a minimap with a cursor, and when the user clicks on something in the map, an information page about it appears on the left. So all frames are interacting through JavaScript.
I know it would normally be better to let the user see that the image is loading, but in my case it gave such a weird view that I thought the visitor would leave quicker when I left it like that.
I now use the code you gave, Fotiman, but instead of hiding the big image I first put it underneath the message using z-index, so the visitor can see progress, then at img.onload the z-indexes swap. I think that's the best solution. thanks again.