Display random image when page loads without utilizing onload in the body tag
Posted
by Peachy
on Stack Overflow
See other posts from Stack Overflow
or by Peachy
Published on 2010-05-05T23:39:55Z
Indexed on
2010/05/05
23:48 UTC
Read the original article
Hit count: 131
I'm trying to create a fairly simple piece of JavaScript that displays a random image from an array each time the page loads. I need to figure out a way to get this running without adding code to the body tag. Is there a way to accomplish this without, say, an onload funtion placed in the body tag?
Here's what I have that relies on the onLoad:
ImageSwitch=new Array();
ImageSwitch[0]='1.jpg';
ImageSwitch[1]='2.jpg';
ImageSwitch[2]='3.jpg';
ImageSwitch[3]='4.jpg';
function swapImage()
{
document.getElementById("theImage").setAttribute("src", ImageSwitch[
Math.round(Math.random()*3)])
}
Any alternative ideas to accomplish this?
Thanks.
© Stack Overflow or respective owner