0

I need to call a method after an image has been loaded and fully rendered (appending it to the dom) in the browser.

In the following example window.onload fire only once, and now after pressing the button.

Any idea how to solve this/

http://jsbin.com/pisuzifofabo/1/

    <!DOCTYPE html>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

    <script>

    window.test = function (){
            var body = document.getElementsByTagName('body')[0];
            body.innerHTML += '<img id="c" src="http://upload.wikimedia.org/wikipedia/commons/3/3c/Ilc_9yr_moll4096.png" height="50" width="50"></img>'
        };

    window.onload = function() {
    alert("load an rendered main");
// call my method
    };

    $('img').load(function(){
        alert('loaded and redered');
    });



    </script>
    </head>
    <body>
    <img id="a" src="http://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg" height="50" width="50"></img>
    <img id="b" src="http://upload.wikimedia.org/wikipedia/commons/e/e3/Big-Bend-NP.jpg" height="50" width="50">
    </img>

    <button id="btn" onClick="window.test();"> click me</button>

    </body>
    </html>
GibboK
  • 64,078
  • 128
  • 380
  • 620

2 Answers2

0

Add onload function to img it will work everytime

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>

window.test = function (){
var body = document.getElementsByTagName('body')[0];
body.innerHTML += '<img onload="callMe()" id="c" src="http://upload.wikimedia.org/wikipedia/commons/3/3c/Ilc_9yr_moll4096.png" height="50" width="50">';
};

window.onload = function() {
    alert("load an rendered main");
};

$('img').load(function(){
    alert('loaded and redered');
});

function callMe() {
    alert("loaded");

}



</script>
</head>
<body>
<img id="a" src="http://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg" height="50" width="50"></img>
<img onload="callMe" id="b" src="http://upload.wikimedia.org/wikipedia/commons/e/e3/Big-Bend-NP.jpg" height="50" width="50">


<button id="btn" onClick="window.test();"> click me</button>

</body>
</html>
murli2308
  • 2,765
  • 3
  • 21
  • 43
0

The following code solved my problem.

Basically I had to adding event handlers after dynamically html was rendered.

http://jsbin.com/tazejuhefixa/1/

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script>

var live = function(){

  $( "img" ).on( "load", function() {
    alert("load and rendered img XX");
});

};

var test = function (){
    console.log('x');
  var $body = $("body");
  $body.append('<img id="c" src="http://upload.wikimedia.org/wikipedia/commons/3/3c/Ilc_9yr_moll4096.png" height="50" width="50"/>');
  live();
  };

$(function() {
  // call my method
  $(document).on("load","img",function(){ // delegate
    alert("load main");
  });
});
</script>
</head>
<body>
<img id="a" src="http://upload.wikimedia.org/wikipedia/commons/3/3f/Fronalpstock_big.jpg" height="50" width="50"/>
<img id="b" src="http://upload.wikimedia.org/wikipedia/commons/e/e3/Big-Bend-NP.jpg" height="50" width="50"/>

<button id="btn" onClick="test();"> click me</button>

</body>
</html>
GibboK
  • 64,078
  • 128
  • 380
  • 620