Sunday 15 April 2012

javascript - Cannot set property 'src' of null -


I am trying to rotate an image using javascript and I am trying to code some which I am looking for But found who I was looking for for. When I run it, it gives me an error: Uncustom Type Error: Property can not be set to 'SRC' tap

It keeps repeating every 5 second intervals between each image The amount of time is

Javascript is here:

  (function () {var rotator = document.getElementById ('rotator'); // Image id var imageDir = ' Images / '; // Change the folder of images var delayInSeconds = 5; // Set number Second delay / Pretty image names var images = ['image2.jpg', 'image3.jpg', 'image4.jpg', 'image1.jpg']; // do not change under this line var num = 0; var changeImage = function ( ) {Var len = images.length; rotator.src = imageDir + images [num ++]; if (num == len) {num = 0;}}; set interval (change image, delay in sequence * 1000);} ) ();   

And here is the html for the image:

  & lt; Img src = "picture / image1.jpg" alt = "rotating image" width = "400" height = "300" id = "rotator" />   

This image1.jpg shows, but not to rotate it. Is there something clearly obvious that I am not seeing?

When in head (see console): < P>

This is because you are announcing to close and execute immediately. When you execute it in head , the DOM is not ready and this img tag still does not exist.

After referencing img , it works:



No comments:

Post a Comment