views:

201

answers:

3

Hi,

I have got a JSP page which loads images dynamically. Now, I also have some "input" elements on this page such as a checkbox and select menus which are being created using a javascript module (creating a different styling for the input elements).

My issue is, one of the images the page is trying to load does not exist on the external link it is trying to open. The server only responds after 15 seconds to say that the image does not exist. This means that I have to wait 15 seconds for my "input" elements to load, since the activation of the javascript module only happens after all images are fully loaded on my page.

Is there a way for me to set a timeout for loading images, such that for example, after 5 seconds of trying to load an image, it stops the request?

Thanks,

Michael

A: 

I think this can't be done as the process of loading images is very deeply entrenched in the browser and can't be accessed or terminated directly.

A possible workaround:

Check after 5 seconds whether the image has proper dimensions yet. If it doesn't, remove the image's DOM element. I'm not sure this will stop loading but I think it should.

Pekka
The thing is, I am creating image objects on the fly, in a loop. Meaning I have to start querying about 10 images each for their dimensions. There is no way to generally check for IMG tags to see if it's still loading?
Michael H
How do you remove an image from the DOM element?basically, straight after calling the (img).load() function in jquery, the request is already in progress. Can it be stopped?
Michael H
There seems to be a image.complete property that is not part of any spec but works cross-browser according to selfHTML, however in different ways: The Netscape/Gecko family sets this to true when *trying* to load an image has been completed (regardless of outcome), IE when an image actually has loaded. Not sure whether that will help you...
Pekka
As to your 2nd question, it should be enough to set the src to null. You would have to try to find out whether that stops the current loading process.
Pekka
+2  A: 

You should use a DOMready event or trigger the module before closing the body tag if you want your scripts to execute before images are loaded/not loaded.

Regarding checking if image exists, you can do something like:

var img = $(new Image()).load(function() {
    // image exists
}).error(function() {
    // image does not exist
}).attr('src','some-image-url.jpg');

This should work cross-browser (it uses the onError event: https://developer.mozilla.org/En/XUL/Image#a-onerror)

David
Thanks David, though this will return some-image-url.jpg only after 15seconds. Since the server returns a 404 only after that amount of time, and not directly
Michael H
Could you please elaborate on the "DOMready event or trigger". Do you have an example?
Michael H
jQuery has a DOMready event that triggers whe the DOM is ready to be manipulated: http://docs.jquery.com/Events/ready. Using this event, you dont have to wait for images like you do with window.onload that you probably use now. You mention that there is a "activation of the javascript module" somewhere... try putting this activation code before closing the body tag for a faster trigger.
David
A: 

Thank you very much for all your answers, you have given me great clues as to what I should have actually done.

In the JavaScript module that creates the "input" elements, the following line was written at the bottom of the script:

window.onload = Custom.init;

I basically took this line out, and wrote it instead in my header.jsp file which is included in all of my pages, in the following way:

$(document).ready(function() {

Custom.init();

});

Thanks again for your quick replies,

Michael

Michael H