View Full Version : javascript more than once on a page...

Sep 13th, 2007, 11:27 AM

I have this script which randomly flicks through images which I want to be able to use more than once on a page so there are lots of images flickering...

trouble is i have no idea how to do it. Anyone got any ideas?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" language="javascript">

var photoNum = Array('1', '2', '3', '4', '5', '6');

var frame_strobe = 0;
var last_strobe;
var timer = 0;
var last_timer;

function strobe(x)
while(frame_strobe == last_strobe){
frame_strobe = Math.floor(Math.random() * 4) + 1;

while(timer == last_timer){
timer = Math.floor(Math.random() * 499) + 1;

last_strobe = frame_strobe;
last_timer = timer;

document.images["blocktest"].src = "http://www.tombrennand.net/testimages/block" + photoNum[frame_strobe] + ".jpg";


cycle = setInterval('strobe(' + x + ')', timer);

return true;

function alternate(image, toggle)

for (id = 0; image != document.images[id]; id++);

if (toggle)

cycle = setInterval('strobe(id)', 1);

return true;

<title>Test Page</title>


<body onLoad="alternate(blocktest, 1);">

<img src="http://www.tombrennand.net/testimages/block1.jpg" alt="Alternating pictures" id="blocktest">


Sep 13th, 2007, 01:49 PM
Here is a link a tutorial on the subject:

Arty Effem
Sep 13th, 2007, 03:17 PM
Here is a link a tutorial on the subject:
http://www.javascriptkit.com/javatutors/multiplejava.shtmlThat is a solution to a different problem, namely initialising multiple different scripts all of which overwrite any existing onload handler.
The only realistic solution is to design object-orientated code to run multiple instances, which is the way all code should be written, except where multiple instances would be impractical or senseless. Even in such cases, it's preferable to avoid the use of global variables.

The closest script I know for this application would be RBanner (http://www.hotspot.freeserve.co.uk/scripterlative?rbanner), which can be configured to randomise the order used for cycling on each page load.

Sep 13th, 2007, 04:38 PM
It does mention declaring the same variable more than once in two different scripts, which would be a problem if you were to try and use the same script twice. Your solution of using an object oriented way would of course be a good way to go.

Sep 13th, 2007, 05:17 PM
thanks for the feedback guys... you seem to be saying everything I had in mind... it's just the technical expertise I don't have!