MDN self-hosted library · e01.org

Navigator: getBattery() method

Battery API

Battery API

The getBattery() method provides information about the system's battery. It returns a battery promise, which resolves with a BatteryManager object providing some properties to get the battery status also some events you can handle to monitor the battery status. This implements the ; see that documentation for additional details, a guide to using the API, and sample code.

Since Chrome 103, the Navigator.getBattery() method of only expose to secure context.

[!NOTE] Access to this feature may be controlled by the Permissions-Policy directive battery.

Syntax

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.

let batteryIsCharging = false;

navigator.getBattery().then((battery) => {
  batteryIsCharging = battery.charging;

  battery.addEventListener("chargingchange", () => {
    batteryIsCharging = battery.charging;
  });
});

For more examples and details, see .

Specifications

Browser compatibility

See also

  • Permissions-Policy battery directive