I'm building a site for myself. I Have made a header background that changes image every time you refresh. But sometimes there comes no image in the header.
Can anyone help?
Javascript:
addEventListener("load",init,false);
function init(event){
var header = document.getElementById('mainheader');
var pictures = new Array(3);
pictures[0]="pictures/test1.jpg";
pictures[1]="pictures/test2.gif";
pictures[2]="pictures/test3.jpg";
var numPics = pictures.length;
if (document.images) {
var chosenPic = Math.round((Math.random() * numPics));
header.style.background = 'url(' + pictures[chosenPic] + ')';
}
}
HTML:
<header id="mainheader">
<a href="index.html"><img src="pictures/untitled.png" alt="logo" id="imglogo"></a>
<nav id="cssmenu">
<ul>
<li class='active'><a href='index.html'><span>Home</span></a></li>
<li><a href='about%20me.html'><span>About Me</span></a></li>
<li><a href='contact.html'><span>Contact</span></a></li>
<li class='last'><a href='pictures.html'><span>Pictures</span></a></li>
</ul>
</nav>