Trying to Create Homepage with Random Background Image Each Time it is Accessed. Using HTML and JavaScript. What am I doing wrong? -
i'm trying make array of multiple background images , having browser choose 1 @ random display.
i have attempted coding don't know i'm going wrong.
here code working with
<head> <meta charset="utf-8"> <title>christopher tameji foose</title> <script src="chrisfoose.js"> var imgsrcarr = ["/background/000.jpg", "/background/001.jpg", "/background/003.jpg"] window.onload = function() { var randnum = math.floor(math.random() * 3); console.log(randnum); document.getelementbyid("main").style.backgroundimage = "url('" + imgsrcarr[randnum] + "')"; } </script> <link rel="stylesheet" href="stylesheet.css"> ;<script> ;</script>
a zip of website @ https://www.sendspace.com/file/2la4he. feedback appreciated.
you have couple of issues here. first, document.getelementbytagname("div.main")
invalid since there no function called "getelementbytagname" want "getelementsbytagname" (notice "s").
second, target div
has id, rather looping through tags , selecting 1 want, use getelementbyid()
instead.
var imgsrcarr = ["/background/000.jpg", "/background/001.jpg", "/background/003.jpg"] window.onload = function() { var randnum = math.floor(math.random() * 3); document.getelementbyid("main").style.backgroundimage = "url('" + imgsrcarr[randnum] + "')";
here fiddle: http://jsfiddle.net/fvo6v0vl/
Comments
Post a Comment