display one image in loop - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Image

Description

display one image in loop

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 = [/*www.j a  v a2s .c om*/
  "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