Jack Nilan         Javascript Tutorial        EMail : jacknilan@yahoo.com

Lesson 7 - Have Random Pictures "Flip" on your Page

You can have random pictures flip on your screen. To do this we will have to call a function over and over again.

In the HEAD we have some script where we declare an Array (vector) and put some images in the vector.

pictures = new Array (3);
pictures[0] = new Image;
pictures[0].src = "picture1.jpg";
pictures[1] = new Image;
pictures[1].src = "picture2.jpg";
pictures[2] = new Image;
pictures[2].src = "picture3.jpg";

function showone()
{var ran = Math.floor(Math.random() * 3);
document.mainpic.src = pictures[ran].src;

To get it started you must call the function from body. In the heading use The onLoad command.

<body onLoad ="showone();">

Up above we put 3 pictures in an array. You can put in as many as you want.
In the main program you must call the showone() function.

There must be an image in your body named mainpic. You can use any name that you like but it must match in body and function

<img src = "somepic.jpg" name = mainpic>

For your assignment write a web page that keeps flashing 1 of 3 pictures randomly on your web page.