Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
Internet Explorer
Events
 onload event
onload event

[This documentation is preliminary and is subject to change.]

Fires immediately after the client loads the object.

HTML 4.01 Specification, Section 18.2.3

Syntax

HTML Attribute <element onload = "handler(event)">
Event Property object.onload = handler;
attachEvent Method object.attachEvent("onload", handler)
addEventListener Method object.addEventListener("load", handler, useCapture)

Standards information

Event information

SynchronousNo
BubblesNo
CancelableNo

Event handler parameters

pEvtObj [in]

Type: IHTMLEventObj

Remarks

The client loads applications, embedded objects, and images as soon as it encounters the applet, embed, and img objects during parsing. Consequently, the onload event for these objects occurs before the client parses any subsequent objects. To ensure that an event handler receives the onload event for these objects, place the script object that defines the event handler before the object and use the onload attribute in the object to set the handler.

The onload attribute of the body object sets an onload event handler for the window. This technique of calling the window onload event through the body object is overridden by any other means of invoking the window onload event, provided the handlers are in the same script language.

Loads the object for which the event is specified.

To invoke this event, do one of the following:

  • Open a document to invoke this event for the document or any object within it.

The pEvtObj parameter is required for the following interfaces:

  • HTMLAnchorEvents2
  • HTMLAreaEvents2
  • HTMLButtonElementEvents2
  • HTMLControlElementEvents2
  • HTMLDocumentEvents2
  • HTMLElementEvents2
  • HTMLFormElementEvents2
  • HTMLImgEvents2
  • HTMLFrameSiteEvents2
  • HTMLInputFileElementEvents2
  • HTMLInputImageEvents2
  • HTMLInputTextElementEvents2
  • HTMLLabelEvents2
  • HTMLLinkElementEvents2
  • HTMLMapEvents2
  • HTMLMarqueeElementEvents2
  • HTMLObjectElementEvents2
  • HTMLOptionButtonElementEvents2
  • HTMLScriptEvents2
  • HTMLSelectElementEvents2
  • HTMLStyleElementEvents2
  • HTMLTableEvents2
  • HTMLTextContainerEvents2
  • HTMLWindowEvents2
  • HTMLDocumentEvents4
  • HTMLElementEvents4

Examples

The following example shows how to declare an onload event for a document that is designed to work with multiple browsers and earlier versions of Internet Explorer.

<!doctype html>
<head>
  <title>Load Event Sample</title>
  <script type="text/javascript">
  function doLoad() {
     alert( "The load event is executing" );
  }
  if ( window.addEventListener ) { 
     window.addEventListener( "load", doLoad, false );
  }
  else 
     if ( window.attachEvent ) { 
        window.attachEvent( "onload", doLoad );
  } else 
        if ( window.onLoad ) {
           window.onload = doLoad;
  }
  </script>
</head>
<body>
  <h1>Load Event Sample</h1>
  <p>This sample demonstrates how to execute 
     an event while the document is loading.</p>
</body>
</html>

See also

a
abbr
acronym
address
applet
area
audio
b
base
baseFont
bdo
bgSound
big
body
blockQuote
br
button
canvas
caption
center
cite
code
col
colGroup
comment
custom
dd
del
dfn
dir
div
dl
document
dt
em
embed
fieldSet
font
form
frame
frameSet
head
hn
hr
html
i
iframe
img
input type=button
input type=checkbox
input type=file
input type=hidden
input type=image
input type=password
input type=radio
input type=reset
input type=submit
input type=text
ins
isIndex
kbd
label
legend
li
link
listing
map
marquee
media
menu
meta
nextID
noBR
noFrames
noScript
object
ol
optGroup
option
p
param
plainText
pre
q
rt
ruby
s
samp
script
select
small
source
span
strike
strong
style
sub
sup
table
tBody
td
textArea
tFoot
th
tHead
title
tr
tt
u
ul
var
wbr
video
window
xmp
SVGSVGElement
Reference
onreadystatechange
onunload

 

 

Build date: 3/14/2012

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
The handler must not be named "onload"!      RalphF ... yecril   |   Edit   |   Show History

The following code has no effect in IE8:

window. onload = function onload () { alert ("Loaded.") }

 

Flag as ContentBug
IE9, Vista gadget, body onload fails if called function is in a separate file      japplis ... japp27   |   Edit   |   Show History

With Internet Explorer 9, in a Vista gadget when Disable Script Debug (Internet Explorer) and Disable Script Debug (Other) are both checked, <body onload="doFunction();"> does not work if doFunction() is in a separate file. If Disable Script Debug (Other) is unchecked and Disable Script Debug (Internet Explorer) is checked it works and also works when both are unchecked.

It always worked in IE8 and previous.

SOLUTION:
This happen only if the javascript files are saved in Unicode without BOM. Saving as Ansi or UTF-8 is all right, Also Unicode with BOM is all right.

Tags What's this?: Add a tag
Flag as ContentBug
SOLUTION: set the onload before you set the src      Mr Wilt   |   Edit   |   Show History
I was confronted with the same problem and spent a day finding a good solution or workaround. Finally I found a solution which worked perfectly using google. The problem is in the order of calling the image.onload and image.src

You need to set the image.onload before you set the image.src. This has to do with the fact that an image that is already in the cache of the browser wil load immediately after setting the image source (thus before the onload event handler is even set), resulting in the onload event handler never to be called at all.

Hope some people are helped by my post

Tags What's this?: Add a tag
Flag as ContentBug
WARNING!!!      george65   |   Edit   |   Show History

For dynamically created img objects, the onload event only fires up if the source file is downloaded form the web server. When the file is already stored on the Internet Explorer's (7) cache, the onload event is never triggered. It seems like there is no work arround for this problem, since the onreadystatechange also fails to trigger when the file already exists on the browser's cache. I don't know if this also happens in IE8, but I do know this only happens with Internet Explorer.

Tags What's this?: bug (x) event (x) img (x) onload (x) Add a tag
Flag as ContentBug
Limitation - window onload fails body resize      Mr. Raymond Kenneth Petry ... Thomas Lee   |   Edit   |   Show History
<SCRIPT for=window EVENT=onload>
alert(obodyid.fireEvent("onresize"));
</SCRIPT>


--fails to fire the body-onresize-event, even though it alerts True.

Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker