Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

How to make a function run when web page loads.

Question posted by: TonyJeffs (Guest) on July 20th, 2005 10:50 AM
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[i]=new Image();
isn[i].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[i]=new Image();
nr[i].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>
rf's Avatar
rf
Guest
n/a Posts
July 20th, 2005
10:50 AM
#2

Re: How to make a function run when web page loads.

"TonyJeffs" <tonyjeffs@aol.com> wrote in message
news:8ec4c623.0309290317.23a577a3@posting.google.c om...[color=blue]
>
> 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.[/color]

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

Cheers
Richard.



TonyJeffs's Avatar
TonyJeffs
Guest
n/a Posts
July 20th, 2005
10:51 AM
#3

Re: How to make a function run when web page loads.
"rf" <making.it.up@the.time> wrote in message news:<kCUdb.129035$bo1.21252@news-server.bigpond.net.au>...[color=blue]
> "TonyJeffs" <tonyjeffs@aol.com> wrote in message
> news:8ec4c623.0309290317.23a577a3@posting.google.c om...[color=green]
> >
> > 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.[/color]
>
> Er,
> <body ... onload="StartIt()">
>
> Cheers
> Richard.[/color]


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

 
Not the answer you were looking for? Post your question . . .
189,088 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors