Load a loadable image to <img> - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

Load a loadable image to <img>

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <title>A test</title> 
   </head> 
   <body> 
      <img src="http://x.invalid" id="myImage"> 
      <script>
var imgs = [/*from  w  w w  .java  2s .  c  o  m*/
  "http://java2s.com",
  "https://www.google.com/images/srpr/logo11w.png",
  "http://www.java2s.com/style/download.png"
];
var imageIndex = 0;
function tryNextImage() {
  var img = document.getElementById("myImage");
  img.onerror = function() {
    imageIndex++;
    tryNextImage();
  }
  img.src = imgs[imageIndex];
}
tryNextImage();

      </script>  
   </body>
</html>

Related Tutorials