javascript array traffic light -
<!doctype html> <html> <body> <img id="h:\it\traffic-light-red.jpg" src="h:\it\traffic-light-red.jpg"> <button type="button" onclick="changelights()">change lights</button> <script> var list = [ "h:\it\traffic-light-red.jpg", "h:\it\traffic-light-red-amber.jpg", "h:\it\traffic-light-green.jpg", "h:\it\traffic-light-red.jpg" ]; var index = 0; function changelights() { index = index + 1; if (index == list.length) index = 0; var image = document.getelementbyid('light'); image.src=list[index]; } </script> </body> </html>
i not understand why code isn't working. every time try open in browser doesn't show me image. i'm struggling in school , appreciate of point me in right direction.
if put "h:\it\traffic-light-red.jpg" in browser's address bar work? nope! cause it's trying access image on file system. need use file protocol. like:
<!doctype html> <html> <body> <img id="h:\it\traffic-light-red.jpg" src="h:\it\traffic-light-red.jpg"> <button type="button" onclick="changelights()">change lights</button> <script> var list = [ "file:///h:/it/traffic-light-red.jpg.html" "file:///h:/it/traffic-light-red-amber.jpg", "file:///h:/it/traffic-light-green.jpg", "file:///h:/it/traffic-light-red.jpg" ]; var index = 0; function changelights() { index = index + 1; if (index == list.length) index = 0; var image = document.getelementbyid('light'); image.src=list[index]; } </script> </body> </html>
if files hosted somewhere on web dropbox grab links , work.
Comments
Post a Comment