codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:

Home » Javascript » Article

Javascript onLoad Event Handler

Article by:  Jeff Anderson  ( 1362 ) (1/22/2003)
Bookmark us now! Add to Favourites
Email a friend!Tell a friend
Summary: Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy
Viewed: 595799 times Rating (103 votes): 
 3.7 out of 5
 Rate this Article  Read Comments  Post Comments

Javascript onLoad Event Handler

Live Demo

Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy. Click here to see an example of an javascript alert that pops up when the page loads in.

How did we do that? Simple : within the body tag of the loading page, put the following:

Select All Code


As usual, keep all the above on one line to ensure it works properly. One thing to watch out for here : don't confuse your single and your double quotes.

Multiple Events

You can easily load multiple events by separating them with semicolons. Alternatively, call a javascript function from the onload event and process as many commands as you like in the function.
<BODY onLoad="javascript:alert('First Action');alert('Second Action');"> 





CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
Rate this article:     Poor Excellent
View highlighted Comments
User Comments on 'Javascript onLoad Event Handler'
Posted by :  vandal at 19:22 on Sunday, November 30, 2003
im tyring to get 2 seperate onload events to work on my webpage but one or the other wont load after the first one has loaded. here is my code,
<BODY bgcolor="#000000" OnLoad="startPix()";"fly()">
one event is a somple backgroud space script and the other is a roatating image script.
im sure i have just missed a simple character or something. if someone could help me and let me know what i am doing stupid i owuld be ever so greatfull
Posted by :  litu at 01:24 on Friday, December 05, 2003
try this instead

<BODY bgcolor="#000000" OnLoad="startPix();fly()">
Posted by :  itus2006 at 22:00 on Sunday, December 28, 2003
This article is normally
Posted by :  Troy Wolf at 22:00 on Friday, January 02, 2004
I agree with itus2006. This article is so normally. In fact, it is more normally than most articles on here. WHAT THE HECK DOES THAT MEAN?! www.engrish.com
Posted by :  tonyf at 14:46 on Tuesday, February 17, 2004
Hi,
I am trying to use the onload event to load a new page in another frame, but whatever I do it always loads in the main frame.

Can anyone help please!!!!
Posted by :  sureshkumar at 01:04 on Wednesday, March 03, 2004
Give target="Framename" on the Onload function then it will load to the frame where u set set the target
Posted by :  etorito35 at 21:46 on Monday, March 08, 2004
does the onload event have to be on every page if it's loading the same images? Or can I just load the rollover image onces into the cache? Will it use the same cached image thoughout the website?

i.e: navegation buttons.

Thanks
David
Posted by :  appineni at 12:00 on Friday, July 09, 2004
Login codes with JSP,JavaBean from mySQL database

can u please help me with this

bye anu
Posted by :  Bdawg8569 at 10:17 on Thursday, October 07, 2004
Hi, i am very new to javascript, and have just one simple line using the window.resizeto function to size my splash page correctly. What i am looking for a way to do, is to have the page become unresizable w/ no scrollbars. I have seen lots of examples of doing this with the window.open or the popup, but this is the first page of my site, so those options i don't like. I was wondering if there was a way to do this using the onload event. Any help would be appreciated.
Posted by :  mahmood at 12:38 on Wednesday, March 02, 2005
i have problem about tab and javascript please help me
Posted by :  hazemmostafa at 09:04 on Thursday, April 14, 2005
hi
I ve these 2 window onload events like that

window.onload=new Function("setTimeout('move()',delay)")
window.onload=new Function("setTimeout('movee()',delayy)")

how can i convert them to body onload ?

thx
Posted by :  chillywylly at 17:08 on Friday, July 01, 2005
Excelent!

But i have some animated gifs and when the main page load(and trigger the scrip) the gif are freezed!

what is wrong?

Thanks!
Posted by :  diedrada at 04:02 on Wednesday, February 01, 2006
Hello, There is a similar code for the onload event ? A code that does exactly the same thing like onload. Thanks !
Posted by :  remotegirl at 17:41 on Tuesday, April 04, 2006
someone help me to work a cousor trailer
Posted by :  Nachtkriecher at 16:57 on Monday, January 15, 2007
yeah but what if yer document is a frame document that doesnt have any <body>? i cant see a good spot to put it....
-Nachtkriecher
Posted by :  abide at 22:52 on Sunday, June 29, 2008
<b>Thanks!</b>
Posted by :  Kan at 11:36 on Friday, February 27, 2009
I have a pop up wndow and I can close it with either a button or with X on top of the page. I have to refresh the parent page when i have a success message on that pop up or else if i have error message then i would have to just close that pop up Can anybody help me with this problem using java script?

Thanks,
Kan
Posted by :  Vince at 11:56 on Wednesday, April 29, 2009
This code is not working on I.E. 8 or Firefox,
does anyone have a fix?

I am tring to load multiple events, and I only get one back in two places?
Posted by :  GS at 00:39 on Thursday, September 17, 2009
im tyring to get 2 seperate onload events to work on my webpage one after the other. Plz any body can send me the appropiate javascript code asap.


To post comments you need to become a member. If you are already a member, please log in .

 



RELATED ARTICLES
Javascript - Enable and Disable form elements
by Jeff Anderson
This is a relatively little known and under-used feature of javascript which can be very useful in guiding a user through a form. Using the disabled tag, you can switch on and off elements in a form.
Check IsNumeric Function
by Jeff Anderson
A javascript validation function to check whether the details entered by a user are numeric.
Javascript Onload Event
by Jeff Anderson
Sometimes you need to perform an action immediatley after the page has loaded. That's when the onLoad Event Handler comes in handy
Form Validation Function
by Jeff Anderson
A javascript validation function that you can use to validate all types of forms.
JavaScript Field Is Empty Form Validation
by Jeff Anderson
This javascript function allows you to check whether a form field has been completed or not.
Javascript Get Selected Text
by Jeff Anderson
A cross-browser script to get text selected by the user
Check Email Validation Function
by Jeff Anderson
A javascript validation function to check whether the user has entered a valid email address in a form.
Multiple submit buttons on a single form
by Kiran Pai
This script shows you how to submit the contents of a form to different programs depending on which Submit button you press. Additionally it also shows how to call two different functions when you press the Submit button.
Validate Form and Disable Submit Button
by Marylou Marks
I have a form that validates info, but I also want to disable the submit button. The disable part worked before adding the form validating.
Javascript onUnload Event Handler
by Jeff Anderson
The onUnload Event Handler allows you to perform an action as the user leaves the page.








Recent Forum Threads
• C++
• Re: refresh parent after closing pop up window
• Dynamic Insertion
• Date and Time function around the world???
• Significant Factors
• Perl array access
• Re: huffman encoding and decoding in C++...
• Perl One Liner: Replace {(
• Re: html including php, accessing the functions


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009