473,398 Members | 2,427 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,398 software developers and data experts.

Preloading images, and getting their height and width (firefox)

Hi,

I want to preload some images for a webpage _and_ determing their width and
height.
The problem is that the scripts continue while the images are loaded in the
background,
while I need the thus undefined values of image.width and image.height.
Thus I'm looking for a function which stops executing my script until the
images are all
fully loaded.
So anyone can help me?

Some non-working sample code included. Notice this only doesn't work well on
Firefox.
The code runs without errors, but it doesn't get the correct height and
width. To see the
problem the images used shouldn't be in the browser cache.

With best regards,
Rik van der Weij

<html>
<head>

<script>
var im_array = new Array(2);
var counter=2;

function LoadImages()
{
// load images
im_array[0] = new Image();
im_array[1] = new Image();

im_array[0].src = "images/1.jpg";
im_array[1].src = "images/2.jpg";

// hook events
im_array[0].onload = CounterMin;
im_array[1].onload = CounterMin;
}

function ShowImages()
{
// 'pre load the images'
LoadImages();

// wait till load complete (doesn't work)
WaitForLoad();

// display images
var t=document.getElementById("output");
t.innerHTML = t.innerHTML + "(h,w) = (" + im_array[0].height + ", " +
im_array[0].height + ")<br>\n";
t.innerHTML = t.innerHTML + "(h,w) = (" + im_array[1].height + ", " +
im_array[1].height + ")<br>\n";
t.innerHTML = t.innerHTML + "<img src='" + im_array[0].src +"'>";
}

function WaitForLoad()
{
if(counter>0)
{
window.setTimeout("WaitForLoad()",250);
}
}

function CounterMin()
{
counter--;
}
</script>

</head>

<body>
<input type="button" onclick="ShowImages()" value="Show Images">
<div id="output">
</div>

</body>

</html>
Jul 23 '05 #1
2 2900
alu

"Van der Weij" <ne**@vanderweij.demon.nl> wrote
Hi,

I want to preload some images for a webpage _and_ determing their width and height.
The problem is that the scripts continue while the images are loaded in the background,
while I need the thus undefined values of image.width and image.height.
Thus I'm looking for a function which stops executing my script until the
images are all
fully loaded.
So anyone can help me?

Some non-working sample code included. Notice this only doesn't work well on Firefox.
The code runs without errors, but it doesn't get the correct height and
width. To see the
problem the images used shouldn't be in the browser cache.

With best regards,
Rik van der Weij

<html>
<head>

<script>
var im_array = new Array(2);
var counter=2;

function LoadImages()
{
// load images
im_array[0] = new Image();
im_array[1] = new Image();

im_array[0].src = "images/1.jpg";
im_array[1].src = "images/2.jpg";

// hook events
im_array[0].onload = CounterMin;
im_array[1].onload = CounterMin;
}

function ShowImages()
{
// 'pre load the images'
LoadImages();

// wait till load complete (doesn't work)
WaitForLoad();
___________________________________

setTimeout does not 'pause' a script,
the script below this keeps right on
going about it's business regardless.
What you will likely need to do is separate
the lines below into a separate function and call
that when your counter reaches 0.
____________________________________

// display images
var t=document.getElementById("output");
t.innerHTML = t.innerHTML + "(h,w) = (" + im_array[0].height + ", " +
im_array[0].height + ")<br>\n";
t.innerHTML = t.innerHTML + "(h,w) = (" + im_array[1].height + ", " +
im_array[1].height + ")<br>\n";
t.innerHTML = t.innerHTML + "<img src='" + im_array[0].src +"'>";
___________________________________

Note that you do not have im_array[x].width anywhere, only
im_array[x].height
___________________________________

}

function WaitForLoad()
{
if(counter>0)
{
window.setTimeout("WaitForLoad()",250);
}
}

function CounterMin()
{
counter--;
}
</script>

</head>

<body>
<input type="button" onclick="ShowImages()" value="Show Images">
<div id="output">
</div>

</body>

</html>



Jul 23 '05 #2
> ___________________________________

setTimeout does not 'pause' a script,
the script below this keeps right on
going about it's business regardless.
What you will likely need to do is separate
the lines below into a separate function and call
that when your counter reaches 0.
____________________________________
I noticed that, fixed my script by changing the display function to
something like

Display {

ok = true
for (item in im_array)
ok=ok && (item.loaded || item.complete);
if (!loaded)
setTimeout(...)
else
display stuff
}

item.loaded is set with a onload hook attached to each image.
Also item.complete is used because IE doesn't fire a onload event when
the image is already cached.
// display images
var t=document.getElementById("output");
t.innerHTML = t.innerHTML + "(h,w) = (" + im_array[0].height + ", " +
im_array[0].height + ")<br>\n";
t.innerHTML = t.innerHTML + "(h,w) = (" + im_array[1].height + ", " +
im_array[1].height + ")<br>\n";
t.innerHTML = t.innerHTML + "<img src='" + im_array[0].src +"'>";


___________________________________

Note that you do not have im_array[x].width anywhere, only
im_array[x].height
___________________________________


It was only a a test script ;)
Jul 23 '05 #3

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

Similar topics

22
by: Fabian | last post by:
var preload1 = new Image(); preload1.src = "/pic/yay.gif"; var preload2 = new Image(); preload2.src = "/pic/nay.gif"; The above is meant to preload image files, yes? Problem is, it doesnt seem...
7
by: Ana | last post by:
I just ran a report on a site I'm redesigning and came up with all these missing height/width tags. How important are they? Should I go through the trouble of making sure they're there? Ana
6
by: michaaal | last post by:
Is this the correct way to preload an image...? Var Image1 = new Image() ....And then when I'm ready to use the image I can do this...? Button1.src=Image1.src ....Or am I just telling...
2
by: windandwaves | last post by:
Hi Gurus Preloading images has got to be JS 101. However, it does not seem to be working. Here is the function that I am using. I added the alerts to make sure it is working and all the right...
1
by: Pums | last post by:
How to get the information(like height, width) of an image to be uploaded?
3
by: littleark | last post by:
Hi everybody, I have a typical javascript images preloader, it works fine both on Firefox and on IE in my intranet (local server). It works fine on the Internet (remote server) in IE. In...
8
by: Kentor | last post by:
Hello, I have users that submit images to my website. By default I choose to make the width bigger than the height because usually pictures are taken horizontally... But in some cases people take...
9
by: K. | last post by:
Witam! Napisalem taka oto funkcje: function img_Pokaz(adres,formName,hint_img) { eval('document.'+formName+'.'+hint_img+'.src = "'+adres+'"'); }
7
by: Keith Hughitt | last post by:
Hi all, I am having trouble preloading images in a javascript application, and was wondering if anyone had any suggestions. Basically I have a bunch of images stored in a database as BLOBs. At...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.