body
elementhtml
element.onafterprint
onbeforeprint
onbeforeunload
onblur
onerror
onfocus
onhashchange
onload
onmessage
onoffline
ononline
onpagehide
onpageshow
onpopstate
onresize
onscroll
onstorage
onunload
interface HTMLBodyElement : HTMLElement { [TreatNonCallableAsNull] attribute Function? onafterprint; [TreatNonCallableAsNull] attribute Function? onbeforeprint; [TreatNonCallableAsNull] attribute Function? onbeforeunload; [TreatNonCallableAsNull] attribute Function? onblur; [TreatNonCallableAsNull] attribute Function? onerror; [TreatNonCallableAsNull] attribute Function? onfocus; [TreatNonCallableAsNull] attribute Function? onhashchange; [TreatNonCallableAsNull] attribute Function? onload; [TreatNonCallableAsNull] attribute Function? onmessage; [TreatNonCallableAsNull] attribute Function? onoffline; [TreatNonCallableAsNull] attribute Function? ononline; [TreatNonCallableAsNull] attribute Function? onpopstate; [TreatNonCallableAsNull] attribute Function? onpagehide; [TreatNonCallableAsNull] attribute Function? onpageshow; [TreatNonCallableAsNull] attribute Function? onresize; [TreatNonCallableAsNull] attribute Function? onscroll; [TreatNonCallableAsNull] attribute Function? onstorage; [TreatNonCallableAsNull] attribute Function? onunload; };
The body
element represents the main
content of the document.
In conforming documents, there is only one body
element. The document.body
IDL attribute provides scripts with easy access to a document's
body
element.
The body
element exposes as event handler
content attributes a number of the event
handlers of the Window
object. It also mirrors
their event handler IDL attributes.
The onblur
, onerror
, onfocus
, onload
, and onscroll
event
handlers of the Window
object, exposed on the
body
element, shadow the generic event
handlers with the same names normally supported by HTML
elements.
Thus, for example, a bubbling error
event dispatched on a child of
the body element of a Document
would first
trigger the onerror
event
handler content attributes of that element, then that of the
root html
element, and only then would it
trigger the onerror
event handler content
attribute on the body
element. This is because
the event would bubble from the target, to the body
, to
the html
, to the Document
, to the
Window
, and the event
handler on the body
is watching the
Window
not the body
. A regular event
listener attached to the body
using addEventListener()
, however, would be run when the
event bubbled through the body
and not when it reaches
the Window
object.
This page updates an indicator to show whether or not the user is online:
<!DOCTYPE HTML> <html> <head> <title>Online or offline?</title> <script> function update(online) { document.getElementById('status').textContent = online ? 'Online' : 'Offline'; } </script> </head> <body ononline="update(true)" onoffline="update(false)" onload="update(navigator.onLine)"> <p>You are: <span id="status">(Unknown)</span></p> </body> </html>