O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Adding a Page Load Event Handler in JavaScript

<script>   function onloadHandler(){     (initialization code here...)   } </script> ... <body onload="onloadHandler()>

To add initialization code that runs when the pages are loaded in JavaScript, create a function in JavaScript that performs the initialization. For example, the following JavaScript code shows a simple skeleton initialization function:

function onloadHandler(){   (initialization code here...) }

To cause the onloadHandler() to trigger when the page is fully loaded, add the event hander function to the onload attribute of the <body> element in the HTML. For example:

<body onload="onloadHandler()> ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required