Click to See Complete Forum and Search --> : Waiting for an image to load
Helix
09-11-2003, 07:52 AM
It shouldn't be hard to make a function that waits for an image to load. I repeatedly check the readyState property of the IMG object until it's "complete", then the function exits. However, the busy loop causes IE to freeze. So further I need either a "sleeping" routine, either some control flow routine like DoEvents - but that's in VB, not even in VBScript, and I'm working with JavaScript. Please help. Here's da code:
function waitforimageload(imageid)
{with (window.top.document.body.all(imageid)){while (readyState!="complete"){}}}
I have two more general questions to ask, as a novice to this forum:
a)What are the disadvantages of VBScripting over JScripting when working with DHTML? My main concern is that some browsers may not have VBScript support (e.g. I noticed IE6SP1 has the installation option "VBscript support", but no JScript - which I believe is suppoted unconditionally).
b)Can you use the Windows API in scripts? It seems to me that you can not (otherwise my above problem would be solved).
Khalid Ali
09-11-2003, 09:32 AM
readyState is IE specifc,so I recomend do not use it as well.
there are couple of image properties that might help you
1. image.onload
2. image.complete
try working with them
JavaScript has the widest support when it comes to client side scripting,hence the benefits are more then using VBScript.
Using windows API from browser will be completely contrary to browsers security modal(since using win API will give you immense control over underlying windows System), so I doubt that you can do that,however, I know that using ActiveXControls a developer can incorporate VB commands,and since that can be done then I am thinking you can call Windows functions using VB..just my guess.
Helix
09-11-2003, 06:10 PM
Thanks for your reply.
Sorry if I'm mistaking, but "onload" is an event (that I was aware of) and "complete" also sounds like an event (I couldn't find it in my older documentation). Anyway, it's not another img property that I'm looking for, nor an img event.
My problem is that image loading is asynchronous. That means that, given the following code:
document.write("<img src=\"blah.jpg\">")
//this is where my "waitforimageload" function must be called
function1(x)
...
...at the time "function1" is called nobody guarantees that the image has finished loading! In my case that is a problem, because I need to get the dimensions of the newly added image (which are badly reported).
So that's why I need to write some function that waits (synchronous execution) for the image to finish loading. I can wait for some property to change or for some event to fire, it's the same to me, but I don't know how to wait - what to put inside the "while" loop, know what I'm saying?
As I said before, IE currently freezes because the image doesn't get the chance to load when using a busy loop (while (readyState!="complete"){}). The loop is infinite. I would have to check the image status and give her some slices of time to load.
Here's the Visual Basic code that would do the job. Maybe it helps.
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
... ... ...
While Not(img.readystate="complete")
Sleep 100
DoEvents
Wend
Thanks again.
Helix
09-14-2003, 01:02 PM
Let me rephrase my question:
"I want to synchronously load an image". That's all. Thank you.