Trigun
Trigun

Reputation: 93

jquery call a function after script load time

i have a page with something like:

link
link
link
link
somethingelse(div)

with my script i change the link in a img and the after i have to edit the somethingelse....

but now i have a problem.. i need the cords of the div but when i try to get it i get the cords BEFORE the img is loaded.... and i get the wrong cords....

how i can run the function after the complete load of the images?

pseudocode:

$("a").each(function(){ replaceAtoIMG(this)}); //change <a> with <img> and add a new image loading
$("div").each(function(){ $(this).position() }); //return value BEFORE the fully completed load

Upvotes: 0

Views: 317

Answers (1)

Palmer
Palmer

Reputation: 78

$('img').on('load', function() {
   // do whatever you want
});

This should solve your on load problem at least.

Sorry, was at work and using phone for stackoverflow so had to be quick.

However, if needing your function to apply to each image on load then you can wrap your on load function inside an each() function. Untested so might need a little fandangling:

$('img').each(function(){
       $(this).on('load', function() {
   // do whatever you want  });  
 });

That should attach that function to all img tags and trigger when theyre done loading. Then you can use their coords or what not youre trying to achieve. Your pseudocode confused me.

Upvotes: 1

Related Questions