Navigator: getBattery() method

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

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 Battery Status API; see that documentation for additional details, a guide to using the API, and sample code.

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

Note: Access to this feature may be controlled by the Permissions-Policy directive battery.

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

desktop mobile server
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Node.js
getBattery
Chrome – Full support
Chrome 38 (Release date: 2014-10-07)
footnote Full support
Edge – Full support
Edge 79 (Release date: 2020-01-15)
footnote Full support
Firefox – No support
Firefox 43 – 51 (Release date: 2015-12-15)
footnote Removed in 52 and later
Opera – Full support
Opera 25 (Release date: 2014-10-15)
footnote Full support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 38 (Release date: 2014-10-08)
footnote Full support
Firefox for Android – No support
Firefox for Android 43 – 51 (Release date: 2015-12-15)
footnote Removed in 52 and later
Opera Android – Full support
Opera Android 25 (Release date: 2014-10-16)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 3 (Release date: 2015-04-10)
footnote Full support
WebView Android – Full support
WebView Android 38 (Release date: 2014-10-08)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
Node.js – No support
Node.js
footnote No support
Secure context required
Experimental
Chrome – Full support
Chrome 103 (Release date: 2022-06-21)
footnote Full support
Edge – Full support
Edge 103 (Release date: 2022-06-23)
footnote Full support
Firefox – No support
Firefox
footnote No support
Opera – No support
Opera
footnote No support
Safari – No support
Safari
footnote No support
Chrome Android – Full support
Chrome Android 103 (Release date: 2022-06-21)
footnote Full support
Firefox for Android – No support
Firefox for Android
footnote No support
Opera Android – Full support
Opera Android 71 (Release date: 2022-09-16)
footnote Full support
Safari on iOS – No support
Safari on iOS
footnote No support
Samsung Internet – Full support
Samsung Internet 20 (Release date: 2023-02-10)
footnote Full support
WebView Android – Full support
WebView Android 103 (Release date: 2022-06-21)
footnote Full support
WebView on iOS – No support
WebView on iOS
footnote No support
Node.js – No support
Node.js
footnote No support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Experimental. Expect behavior to change in the future.

See also