Events

The

window
object contains the HTML document and has top-level events that make sense for this purpose:

Event Description
onblur
Fired when the window object loses the focus
onfocus
Fired when the window object gains the focus
onhelp
Fired when the user presses the F1 or Help key
onbeforeunload
Fired before the browser window is closed
onresize
Fired when the browser window is resized
onload
Fired when the HTML text for the page has finished loading
onunload
Fired just before the page is unloaded
onscroll
Fired when the user scrolls a page or element
onerror
Fired when an error occurs loading a document or image

OnLoad, OnUnload, onBeforeUnload

These events are very useful for executing any code that needs to coincide with the loading or unloading of the browser.

onLoad
is fired when the HTML for the current page has been downloaded, but may fire before all images, controls, or applets have finished loading.

The

onUnload
event is not only fired before the browser navigates to a new page, but also immediately before the browser is shut down (which is understandable, because the page is indeed being unloaded in preparation for application shutdown).

Finally,

onBeforeUnload
, fires before the page is unloaded. This event is only used during data binding and it allows you to validate entries in a field, before they're sent to a server, or pop a message box up. Its uses are discussed in the data binding section of the book in chapter 8.

OnFocus and OnBlur

When the user of the browser switches to another application or instance of IE4,

onBlur
is fired. Any code placed in this event will be executed before the switch to the new window is made. The converse of
onBlur
is
onFocus
. This event is fired in the current browser when the user switches back to the browser window from another application. While
onFocus
and
onBlur
existed in the Internet Explorer 3.0 object model for some form elements, their appearance in the object definition for the
window
object didn't happen until IE4.

OnResize

This event is rather simple compared to some of the other events we've just finished considered. When the browser window is resized, this event is fired.

OnHelp

If a user presses F1 (or another key that happens to be mapped to online help), the window's

onHelp
event is fired. This gives the developer control over what will happen if a person browsing their page requests help. The code for this event could redirect the user to one or more help pages on the same or another server on the internet/intranet. The new page can be displayed in a new window, in a modal dialog, or in the same browser window by using the
open
,
showModalWindow
, or
navigate
methods of the
window
object, respectively.

OnScroll

If not all of the display is visible to the user, and the user has to scroll the display to view it, in its entirety then an

onscroll
event is generated.

OnError

This is perhaps one of the most useful events. It can be used if an error occurs during the download of a page or image. It can be used to determine whether an error occurred to the connection or whether the download was actually aborted. The user can allow for this eventuality within script and decide whether to get the script to try and download the information again, or display an appropriate message and take corrective action.

© 1997 by Wrox Press. All rights reserved.