JavaScript & IE7 - Why won't my *.onload = function() { } fire?

I have a gallery I quickly coded up for a small site, and under Firefox 3 and Safari 3 works fine. But when I test on my old best friend IE7, it seems to not fire the imageVar.onload = function() { // code here }.. which I want to use to stop the load effect and load the image.

Please bear in mind...

  • I know the thumbnails are just scaled down version of the larger images. When the images are finalised by the client I am going to create proper thumbnails.
  • This is my first attempt to try and get out of procedural JavaScript for the most part.. so please go easy and kindly let me know where my code sucks!

Answers


For successful use of Image.onload, you must register the event handler method before the src attribute is set.

Related Information in this Question:

Javascript callback for Image Loading


Cross browser event support is not so straightforward due to implementation differences. Since you are using jQuery at your site, you are better off using its events methods to normalize browser support:

instead of:

 window.load = function(){ 
      //actions to be performed on window load
 }

 imageViewer.image.onload = function(){ 
     //actions to be performed on image load
 }

Do:

$(window).load(function(){ 
    //actions to be performed on window load
});

 $(imageViewer.image).load(function(){ 
      //actions to be performed on image load
 });

Just to add to the suggestion by Eran to use the jQuery's built in event handlers you can run code when the document is loaded and the DOM is created but before the images are downloaded with:

$(document).ready(function(){
   //your code
});

Need Your Help

websocket library with easy upgrade path to .net4.5 websockets

c# .net .net-4.0 websocket .net-4.5

I'm looking to add WebSocket support to an existing comet server implementation, and I am wondering if there are any reliable/stable .Net4 WebSocket libraries which will offer relatively little pai...

Print 1 to 100 using 10 threads in java

java multithreading

I'm new to muti-threading and I got a question to print 1 to 100 using 10 threads in Java with below constrain.