Latest web development tutorials

onload event

Event Object Reference Event objects

Examples

When the page has finished loading execute Javascript code:

<body onload="myFunction()">

try it"
(Bottom of the page to see more examples)

Definition and Usage

onload event occurs immediately after the page or image loading is complete.

onload usually used <body> element when the page is fully loaded (including images, css files, etc.) execute script code.

grammar

In HTML:

<body onload="SomeJavaScriptCode">

In JavaScript:

window.onload=function(){SomeJavaScriptCode};

参数 描述
SomeJavaScriptCode 必需。规定该事件发生时执行的 JavaScript。


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support onload event


The following HTML tags support onload:

<body>, <frame>, <frameset>, <iframe>, <img>, <input type="image">, <link>, <script>, <style>


Examples

More examples

In the <img> use onload
Photos pop "Image is loaded" immediately after loading


Event Object Reference Event objects