View Full Version : pre-loading large jpg's
AstroChicken
12-30-2003, 11:30 AM
is it possible to pre-load a single large jpg using regular html/javascript? it isnt any special sort of mouseover image or applet or anything. its just a regular <IMG> picture on a basic <HTML> page. I would ideally like to have a 'loading...' message displayed in place of the image while it is loading.
i know this is probably a long shot.
thanx
Willy Duitt
01-01-2004, 05:36 PM
Hi AstroChicken;
Welcome to HTML Forums :D
Try this:
<script type=text/javascript>
<!-- Begin Hide //
var myImage = new Image();
myImage.src = 'http://www.stadiumsofnfl.com/afc/aldelph102.jpg';
function chkLoad() {
if (myImage.complete) {
document.getElementById('theImage').innerHTML='<img src="http://www.stadiumsofnfl.com/afc/aldelph102.jpg"'+
'width="640" height="480" alt="Adelphia Coliseum">';
clearTimeout(isLoaded);
}
if (!myImage.complete) {
document.getElementById('theImage').innerHTML='<table width="640" height="480">'+
'<tr><td align="center" valign="middle">Loading Image....</td></tr></table>';
isLoaded = setTimeout('chkLoad()',100);
}
}
// End Hide -->
</script>
</HEAD>
<BODY text="#36d5ff" bgColor="#000000" onload="chkLoad()">
<CENTER>
<BR><BR>
<FONT size=7 color=#666666><B>Image Loading Test</B></FONT><BR />
<BR><BR>
<div id="theImage" width="640" height="480"></div>
</CENTER>
</BODY>
</HTML>
.....Willy
Edit: Added clearTimeout to stop function from firing when condition is met.
AstroChicken
01-02-2004, 09:44 AM
Sorry Willy, that script doesn't work. I copied and pasted the exact thing into a new HTML file and ran it. everything worked like the picture loaded after a certain time and everything, it just didnt display the words 'loading image'.....i didn't have to change anything on it did i?
thanks again
Willy Duitt
01-02-2004, 10:44 AM
Sorry Willy, that script doesn't work.
Don't tell me it doesn't work.
If it doesn't work for you.
You did something wrong!
Here (http://www.transload.net/~cyberrite/Temp/Post/chkLoad.html) is an example which will be up for a short time.
Next time, post a link to your page
so someone can see what is wrong.
.....Willy
o_O sweeet! your script rocks in IE6, mozilla 1.5, firebird 0.7, and opera 7 all of which im running on windows xp pro :D
i just found this site (http://caucuscare.com/~roth/JAVASCRIPT/refp_165.htm) which has some extra info on the other properties, methods and events on the image object in javascript...
AstroChicken: perhaps you're running an older browser ( like ie2 ) or a different browser that doesn't support the .complete property on image objects in javascript... --or-- the image(s) you are pre-loading are too small and load fast before the loading text can appear( i had to make a 5500x2048 image in paint to get the script to take a while to d/l the image on my cable modem, but it works perfectly )...
at any rate; i'm in full agreement with willy, so if it's not any of those possibilities, please post your code so we can see what else it could be...
Willy Duitt
01-03-2004, 08:40 AM
Originally posted by AstroChicken
everything worked like the picture loaded after a certain time
and everything, it just didnt display the words 'loading image'.....
Hi UCM;
I think you missed AstroChicken's mention that the image loaded,
but the Image Loading.... <TABLE> didn't.
If AstroChcicken's browser did not support the .complete
property. The image would not have loaded. And by default,
the <TABLE> including the Image Loading... should have.
Since the table which I used to prevent the division from
collapsing until the image loaded did not load, I can only
assume that this line was somehow corrupted.
document.getElementById('theImage').innerHTML='<table width="640" height="480">'+
'<tr><td align="center" valign="middle">Loading Image....</td></tr></table>';
FWIW: The true limitation to this script is the use of
getElementById which only CSS1+ level browsers support.
(IE5.5+; NS6+)
However, if someone was willing to use another small byte
Image Loading... image. It would be easier and more compliant
with all browsers to swap the .src until the is.complete condition is met.
And, using the image Array, this method could be used for all images on the page.
.....Willy
agent002
01-03-2004, 08:57 AM
Originally posted by Willy Duitt
FWIW: The true limitation to this script is the use of
getElementById which only CSS1+ level browsers support.
(IE5.5+; NS6+)
IE 5.0 supports getElementById() as well.
Willy Duitt
01-03-2004, 09:05 AM
Originally posted by agent002
IE 5.0 supports getElementById() as well.
Thank You; That's good to know. ;)
I had thought so, but wasn't positive.
So I erred on the side of caution. :)
.....Willy
Willy Duitt
01-03-2004, 03:08 PM
FWIW: To swap one image. The below example using the
.src swap should be supported by all browsers. Just use
a quick loading small byte image to swap with until the
larger image loads.
<script type="text/javascript">
<!-- Begin Hide //
var myImage = new Image();
myImage.src = 'LARGE.jpg';
function chkLoad() {
if (myImage.complete) {
document.images['theImage'].src='LARGE.jpg';
clearTimeout(isLoaded);
}
if (!myImage.complete) {
document.images['theImage'].src='SMALL.jpg';
isLoaded = setTimeout('chkLoad()',100);
}
}
// End Hide -->
</script>
</HEAD>
<BODY onload="chkLoad()">
<img name="theImage" src="" alt="This is a picture">
And, looping thru an image array. The following should
Post-Load all of the document.images to enable the page
to quickly render and display the "Images Loading" image,
while the larger images load. Thus, allowing a quick page
display even for the most graphic intensive documents.
(Note: This has not been fully tested with real images)
Change what is highlighted in red to reflect your images.
<script type="text/javascript">
<!-- Begin Hide //
imagesLoading = new Image();
imagesLoading.src = 'MY_SMALL_IMAGES_LOADING.jpg?';
theImages = ('img1.jpg,img2.jpg,img3.jpg,img4.jpg,img5.jpg,img6.jpg,img7.jpg');
function chkLoad() {
theImages = theImages.split(',');
for(i=0;i<theImages.length;i++) {
myImage = new Image();
myImage.src = theImages[i];
if (document.images[i].complete) {
document.images[i].src = myImage.src;
clearTimeout(isLoaded);
}
if (!document.images[i].complete) {
document.images[i].src = imagesLoading.src;
isLoaded = setTimeout('chkLoad()',100);
}
}
}
// End Hide -->
</script>
</HEAD>
<BODY onload="chkLoad()">
<img src="" alt="This is a picture">
<img src="" alt="This is a picture">
<img src="" alt="This is a picture">
<img src="" alt="This is a picture">
<img src="" alt="This is a picture">
<img src="" alt="This is a picture">
<img src="" alt="This is a picture">
<img src="" alt="This is a picture">
.....Willy
Edit: Added a querry ? to the Images Loading Image to prevent
any possible conflict with the image being loaded numerous times.
(Note: This may need to be removed if it prevents caching of the image)
Willy Duitt
01-03-2004, 06:47 PM
http://www.transload.net/~cyberrite/Images/Gif/Sports/T_titans.gif are kickin Baltimores butt! :D
BTW: ^^ The second script ^^ doesn't work as intended.
There is a problem with splitting the array. Feel free to work with it.
....Willy
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.