-1

hi i'd like to preload an image using jquery. After the image is preloaded there should be an alert message: "image preloaded"

i tried the following code but it doesn't work:

$("images/test.jpg").load(function(){
  alert("Image loaded.");
});

hope u can help.

  • 1
    `$("images/test.jpg")` ??? That's not how it works, please start from the beginning, reading some tutos – A. Wolff May 22 '14 at 16:34

4 Answers4

0

Have a look at this plugin for JQuery: http://plugins.jquery.com/imagesloaded/

You can fire off an event just as you propose as long as your selector is scoped correctly:

$('#container').imagesLoaded( function() {
  // images have loaded
});
thinice
  • 700
  • 5
  • 19
0

You will need an id which will contain that image.

$( "#div" ).load( "images/test.jpeg", function() {
alert( "Load was performed." );
});

See the documentation at http://api.jquery.com/load/

Khushal Dave
  • 507
  • 5
  • 19
0

Seems you want:

$("img[src='images/test.jpg']").load(function(){
  alert("Image loaded.");
});

Now what about reading the DOC: http://api.jquery.com/load-event/ ???

That's said, be aware of cache behaviour, especially on older browsers.

A. Wolff
  • 74,033
  • 9
  • 94
  • 155
0

the following solution worked out for me:

$('<img src="images/test.jpg">').load(function() {
      alert("Image loaded.");
});

i found it here: https://stackoverflow.com/a/10863680/1164027

Community
  • 1
  • 1