1

i want to execute a Command after finish loading a Div. Should be easy, but i couldn´t handle it.

//With window it works fine
$(window).load(function()
{
 alert($("#firstAmazon").attr("class"));
});

//With an other Object not
$("#firstAmazon").load(function()
{
 alert("firstAmazon loaded");

});
<div class="socialIcon" id="firstAmazon">
 <a href="amazon" title="Amazon">
 <img src="../img/socialBar/amazon.png">
 </a>
</div>

That comment was very helpful

div elements do not raise a load event, only the document, window and img elements do. – Rory McCrossan 16 mins ago

Actually i want to execute teh command after loading the img (amazon.png), So document ready don´t help me.

it works not like the documentation. I want that the load() fires after the Image is loaded.

$("#firstPic").load(function()
{
 alert("firstPic loaded");

});
<div class="socialIcon">
 <img  id="firstPic" src="http://www.smartturtle.com/img/header.jpg">
</div>

here the documentation http://api.jquery.com/load-event/

$( "#book" ).load(function() {
  // Handler for .load() called.
});
<img src="book.png" alt="Book" id="book">

4 Answers4

0

The div elements do not raise a load event.

Use document.ready handler as shown :-

$(function(){
   alert("firstAmazon loaded");
});

The document.ready handler executes after the DOM is completely loaded.

Kartikeya Khosla
  • 18,039
  • 8
  • 39
  • 64
0

Use this document ready function()

$(document).ready(function(){
   alert("firstAmazon loaded");
});
I'm Geeker
  • 4,642
  • 5
  • 19
  • 40
0

You can also try this way to check div loaded

var interval;
window.onload = createInterval(); 

function createInterval(){ 
   interval=setInterval(readyCheck(),100); 
} 

function readyCheck(){ 
  if($('#firstAmazon img').get(0).complete){ 
     clearInterval(interval); 
     alert('firstAmazon loaded'); 
  } 
  else { 
     alert('firstAmazon loading');
  } 
} 
111
  • 1,731
  • 1
  • 11
  • 15
0

Try This

<script type="text/javascript">
                jQuery(document).ready(function($) {

                    $(window).load(function(){
                      alert($("#firstAmazon").attr("class"));
                    });

                });
            </script>