HTMLBodyElement
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
TheHTMLBodyElement interface provides special properties (beyond those inherited from the regularHTMLElement interface) for manipulating<body> elements.
In this article
Instance properties
Inherits properties from its parent,HTMLElement.
HTMLBodyElement.aLinkDeprecatedA string that represents the color of active hyperlinks.
HTMLBodyElement.backgroundDeprecatedA string that represents the description of the location of the background image resource. Note that this is not a URI, though some older version of some browsers do expect it.
HTMLBodyElement.bgColorDeprecatedA string that represents the background color for the document.
HTMLBodyElement.linkDeprecatedA string that represents the color of unvisited links.
HTMLBodyElement.textDeprecatedA string that represents the foreground color of text.
HTMLBodyElement.vLinkDeprecatedA string that represents the color of visited links.
Instance methods
No specific methods; inherits methods from its parent,HTMLElement.
Event handlers
TheHTMLElement events are inherited.
The followingWindowonXYZ event handler properties are also available as aliases targeting thewindow object. However, it is advised to listen to them on thewindow object directly rather than onHTMLBodyElement.
Note:UsingaddEventListener() onHTMLBodyElement will not work for theonXYZ event handlers listed below. Listen to the events on thewindow object instead.
HTMLBodyElement.onafterprintFired after the associated document has started printing or the print preview has been closed.
HTMLBodyElement.onbeforeprintFired when the associated document is about to be printed or previewed for printing.
HTMLBodyElement.onbeforeunloadFired when the window, the document and its resources are about to be unloaded.
HTMLBodyElement.onblurFired when the window loses focus.
HTMLBodyElement.onerrorFired when an error occurs and bubbles up to the window.
HTMLBodyElement.onfocusFired when the window gains focus.
HTMLBodyElement.ongamepadconnectedFired when the browser detects that a gamepad has been connected or the first time a button/axis of the gamepad is used.
HTMLBodyElement.ongamepaddisconnectedFired when the browser detects that a gamepad has been disconnected.
HTMLBodyElement.onhashchangeFired when the fragment identifier of the URL has changed (the part of the URL beginning with and following the
#symbol).HTMLBodyElement.onlanguagechangeFired when the user's preferred language changes.
HTMLBodyElement.onloadFired when the document has finished loading.
HTMLBodyElement.onmessageFired when the window receives a message, for example from a call to
Window.postMessage()from another browsing context.HTMLBodyElement.onmessageerrorFired when the window receives a message that can't be deserialized.
HTMLBodyElement.onofflineFired when the browser has lost access to the network and the value of
Navigator.onLineswitches tofalse.HTMLBodyElement.ononlineFired when the browser has gained access to the network and the value of
Navigator.onLineswitches totrue.HTMLBodyElement.onpagehideFired when the browser hides the current page in the process of presenting a different page from the session's history.
HTMLBodyElement.onpageshowFired when the browser displays the window's document due to navigation.
HTMLBodyElement.onpopstateFired when the active history entry changes while the user navigates the session history.
HTMLBodyElement.onrejectionhandledFired whenever a JavaScript
Promiseis rejected and the rejection has been handled.HTMLBodyElement.onresizeFired when the document view has been resized.
HTMLBodyElement.onscrollFired when the document view or an element has been scrolled.
HTMLBodyElement.onstorageFired when a storage area (
localStorage) has been modified in the context of another document.HTMLBodyElement.onunhandledrejectionFired whenever a
Promiseis rejected but the rejection was not handled.HTMLBodyElement.onunloadFired when the document is being unloaded.
Note that whileonblur,onerror,onfocus,onload,onresize, andonscroll are available on any element, their meanings on the<body> element are not the same as on other elements. They listen for events on thewindow object instead.
Specifications
| Specification |
|---|
| HTML> # htmlbodyelement> |
Browser compatibility
See also
- HTML element implementing this interface:
<body>