How to make a function run when web page loads.

T

TonyJeffs

I'm new to this, and stole/doctored my code from the "Ugly JavaScript
Handbook"

I have a slide show program.
First theres stuff to set up the array, and the time between slides
Then a function startIt()is defined
then a function stopIt()
Then two buttons, Start and Stop, which when pressed call the relevant
functions

When the page loads, you have to click the Start button to get the
slide show running, but I'd like it to start running straight away
without having to click.
I can't for the life of me figure this out.
I thought it was just a question of putting the line
startIt();
in the appropriate place.

Here's the whole of the code

Thanks

Tony

<HTML><HEAD>
<TITLE>Holiday</TITLE>

<SCRIPT LANGUAGE="JavaScript">
<!-- Hide from JavaScript-Impaired Browsers
var ctr=1;
var pics=17;
var speed=3500; // set initial
var ns="0123456789"; // number index
/* First, create a new array and then
create an image holder and load it
with the logo animation images. */
isn=new Array();
startIt()
for (i=1;i<=pics;i++){
isn=new Image();
isn.src=i+".jpg";
}
startIt()
/* Next do the same for the number
images to display speed. */
nr=new Array();
for (i=0;i<=pics;i++){
nr=new Image();
nr.src="nr/"+i+".jpg";

startIt()
}

function startIt(){
document.ugly.src=isn[ctr].src;
ctr=(ctr>(pics-2)?1:++ctr);
/* Here we put a "hook" on the
setTimeout() call by naming it.
This allows the clearTimeout()
call to know which one (there
might be others) to stop. */
tstop=setTimeout("startIt()",speed);
}
startIt()

function stopIt(){
clearTimeout(tstop);
}
startIt()
// End Hiding -->
</SCRIPT>
startIt()
</HEAD>
<BODY BGCOLOR="white"><CENTER>
<P><B>Ginny & Eva tour Europe</B>
<P><IMG SRC="5.jpg" height="350" NAME="ugly" BORDER="0">
<P><FORM>


<P><INPUT TYPE="button" NAME="b1" onClick="startIt()" VALUE=" Start
Slide Show">
startIt();
<INPUT TYPE="button" NAME="b2" onClick="stopIt()" VALUE=" Stop Slide
show">
</FORM>
startIt()
<P>Present delay (milliseconds):
<IMG SRC="nr/1.gif" WIDTH="7" HEIGHT="10" BORDER="0">
<IMG SRC="nr/0.gif" WIDTH="70" HEIGHT="10" BORDER="0">
<IMG SRC="nr/0.gif" WIDTH="70" HEIGHT="10" BORDER="0">
<IMG SRC="nr/0.gif" WIDTH="7" HEIGHT="10" BORDER="0">

startIt()
</BODY>
startIt()
</HTML>
 
R

rf

TonyJeffs said:
When the page loads, you have to click the Start button to get the
slide show running, but I'd like it to start running straight away
without having to click.

Er,
<body ... onload="StartIt()">

Cheers
Richard.
 
T

TonyJeffs

rf said:
Er,
<body ... onload="StartIt()">

Cheers
Richard.


Thanks
There's a logic to that!
Maybe I should buy a book to get me underway.
Does JS have public variables, and scope and stuff like that?

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top