MDN Web Docs

Syntax

js

getBattery()

Parameters

None.

Return value

A Promise that fulfills with a BatteryManager object which you can use to get information about the battery's state.

Exceptions

NotAllowedError DOMException

Use of this feature was blocked by a Permissions Policy.

SecurityError DOMException

The User Agent does not expose battery information to insecure contexts and this method was called from an insecure context.

Examples

This example fetches the current charging state of the battery and establishes a handler for the chargingchange event, so that the charging state is recorded whenever it changes.

js

let batteryIsCharging = false;
navigator.getBattery().then((battery) => {
  batteryIsCharging = battery.charging;
  battery.addEventListener("chargingchange", () => {
    batteryIsCharging = battery.charging;
  });
});

For more examples and details, see Battery Status API.

Specifications

Specification
Battery Status API
# dom-navigator-getbattery

Browser compatibility

See also

Help improve MDN

Yes No

Learn how to contribute

This page was last modified on by MDN contributors.

Read the original on developer.mozilla.org ↗