Jason
Jason

Reputation: 100

jQuery load event with my code

Ok i have some jquery javascript which i intend on adding a .load() event to, hopefully someone can assist me.

$(function() {

  $('#nav li a img').each(function() {
   var originalSrc = this.src,
       hoverSrc = originalSrc.replace(/\.(gif|png|jpe?g)$/, '_over.$1'); 
       image = new Image();

   image.src = hoverSrc;

   $(this).hover(function() {
      this.src = hoverSrc;
   }, function() {
      this.src = originalSrc;
   });
  });
})

Upvotes: 0

Views: 97

Answers (1)

alex
alex

Reputation: 490143

Before you set the src property of the Image, attach the callback like so...

image.onload = function() {
  // The image has loaded successfully.
}

When the image has loaded and is successful, it will call that function.

If the function is not successful, it will call the function assigned to the onerror property.

Alternatively, you can use $(image).load(function() { ... }).

Upvotes: 2

Related Questions