The Window object supports many events. Unfortunately, many of these are proprietary. The safe cross-browser window events include onblur, onerror, onfocus, onload, onunload, and onresize and are detailed in Table 12-2.
Event |
Description |
---|---|
onblur |
Fires when the window loses focus. |
onerror |
Rudimentary error handling event fired when a JavaScript error occurs. |
onfocus |
Fires when the window gains focus. |
onload |
Fires when the document is completely loaded into the window. Warning: Timing of this event is not always exact. |
onresize |
Event triggered as user resizes the window. |
onunload |
Triggered when the document is unloaded, such as following an outside link or closing the window. |
Adding Window events handlers can be set through HTML event attributes on the <<body>> element like so,
<<body onload="alert('entering window');" onunload="alert('leaving window')">>
or by registering events through the Window object:
function sayHi() { alert('hi'); } function sayBye() { alert('bye'); } window.onload = sayHi; window.onunload = sayBye;
Internet Explorer and Netscape add numerous events to the Window object. A few of the more useful ones are detailed in Table 12-3. A general discussion of Window events can be found in Chapter 11 with a complete listing in Appendix B.
Event |
Description |
---|---|
onafterprint |
Event triggered after the window is printed. |
onbeforeprint |
Fires just before the window is printed or print previewed. |
onbeforeunload |
The event is triggered just before the window unloads. Should happen before the onunload event. |
ondragdrop |
Is triggered when a document is dragged onto a window. (Netscape only.) |
onhelp |
Fires when the Help key, generally F1, is clicked. |
onresizeend |
Fires when the resize process ends—usually the user has stopped dragging the corner of a window. |
onresizestart |
Fires when the resize process begins—usually the user has started dragging the corner of a window. |
onscroll |
Fires when the window is scrolled in either direction. |