window.onload example 3: Correct usage | WebReference

window.onload example 3: Correct usage

window.onload example 3: Correct usage

This example uses an external script specified by writing out a script tag with src via document.write. Note that the assignment to window.onload = init; occurs outside of the script block where the script tag is written out. This works because the init function used to initialize window.onload is defined in the previous script block and is available in subsequent script blocks.

This example works as expected by displaying an alert in both Netscape 6/Mozilla or Internet Explorer.