Jquery .width() returns 0 zero before appending the image to the browser - Javascript jQuery

Javascript examples for jQuery:Width Height

Description

Jquery .width() returns 0 zero before appending the image to the browser

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.6.2.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){/* w w  w  .j a  v  a  2s .c om*/
$("#img").attr("src", "http://www.java2s.com/style/demo/Google-Chrome.png").load( function() {
    console.log($(this).width() + " is the width of the image :)");
});
    });

      </script> 
 </head> 
 <body> 
  <img id="img" src="http://www.java2s.com/style/demo/InternetExplorer.png"> 
  <img src="http://www.java2s.com/style/demo/Firefox.png">  
 </body>
</html>

Related Tutorials