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()> ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.