View Full Version : Why does this script fail only in safari?

05-05-2004, 06:44 AM
The javascript works with IE mac and PC, and Mozilla on PC.
I can't find anyone who has documented quirks like this.

Lend me a hand if you can. Thanks!


Call this function on any page with images. It'll track whether they've loaded.

function progress() {
doneLoaded = 0;
fraction = 100/(imgDivs.length + 1);
for (var i = 0; i < document.images.length; i++) {
img = document.images[i];
// alert(i);
if (img.complete == true) {
alert('img#_' + i);
doneLoaded += fraction;
if (doneLoaded < 100) { setTimeout( "progress()", 100); }


see the script in action:

if all images aren't loaded, the script begins again from zero, but takes no time to catch up to where it left off.

I use this in conjunction with another script that displays the progess of loading.

script with alerts for debuggin
(12 imgs, might loop twice, but not infinitely...promise)

05-05-2004, 07:08 AM
Maybe Safari doesn't support Image.complete property. Type this in the address bar to quickly verify:

javascript:alert(typeof new Image().complete)

It should alert boolean if supported and undefined if not.

If indeed Safari doesn't support that property, try this solution:

Drop the progress() function and just put this line outside a function:

var fraction = 100/(imgDivs.length + 1);

Then put an onload event handler in each of the <img> tags. I assume Safari supports onload, which it should.

<img id="eshMD" class="imgDiv" style="left:40px;" src="images/eshMD.jpg" alt="Appeal" onload="doneLoaded+=fraction" />

Then the script at the bottom should only call the unLoad('bar')

<script language="JavaScript" type="text/javascript" >

05-05-2004, 08:01 AM
Thanks. I didn't know I could test code in the address bar.

Oddly enough, Safari returned boolean.

It makes no sense. Safari will reach the first alert of my script, but won't return true. I don't own a mac anymore, which makes this difficult for me to troubleshoot. (and annoys my friends)

I originally used <img onload="..."> as you suggested. But it's not standards compliant, so Mozilla won't do it. Onload is only legal with body and frameset.

Img.complete was my mostly successful attempt to keep the code legit.

If you have any other suggestions...

Thanks again for digging into this.

05-05-2004, 08:35 AM
Do you see any errors in the javascript console of safari?
If the Debug menu of safari is not visible, read this JS FAQ (http://www.codingforums.com/showthread.php?p=178016#post178016) (at the bottom part of the post) for instruction of turning it on.

05-05-2004, 07:05 PM
I don't have regular access to Safari.

Can anyone out there run this code for errors?

It'd be much appreciated...