Navigator.battery

  • Revision slug: Web/API/window.navigator.battery
  • Revision title: window.navigator.battery
  • Revision id: 406897
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Remove details that are explained more fully in the complete docs for attributes.Moved From DOM/window.navigator.battery to Web/API/window.navigator.battery

Revision Content

{{ domref() }}

The battery object provides information about the system's battery charge level; you can also listen to events it sends that provide updates about the battery's charge status. This implements the Battery Status API; see that documentation for additional details, a guide to using the API, and sample code.

Attributes

{{domxref("window.navigator.battery.charging", "charging")}} {{ReadOnlyInline}}
A Boolean value indicating whether or not the battery is currently being charged.
{{domxref("window.navigator.battery.chargingTime", "chargingTime")}} {{ReadOnlyInline}}
A number representing the remaining time in seconds until the battery is fully charged, or 0 if the battery is already fully charged.
{{domxref("window.navigator.battery.dischargingTime", "dischargingTime")}} {{ReadOnlyInline}}
A number representing the remaining time in seconds until the battery is completely discharged and the system will suspend.
{{domxref("window.navigator.battery.level", "level")}} {{ReadOnlyInline}}
A number representing the system's battery charge level scaled to a value between 0.0 and 1.0.

Events

You can listen to the battery object to receive these events that let you know when the battery charge state has changed.

chargingchange
Fires when the battery charging state is updated.
chargingtimechange
Fires when the battery charging time is updated
dischargingtimechange
Fires when the battery discharging time is updated.
levelchange
Fires when the battery level is updated.

Specifications

Specification Status Comment
{{SpecName('Battery API')}} {{Spec2('Battery API')}} Initial specification.

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ Unimplemented_inline_webkit("90538") }} {{CompatGeckoDesktop("10")}} {{ property_prefix("moz") }} [1]
{{CompatGeckoDesktop("16")}} (without prefix) [2]
{{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatUnknown() }} {{ CompatNo() }} {{CompatGeckoMobile("10")}} {{ property_prefix("moz") }} [1]
{{CompatGeckoMobile("16")}} (without prefix) [2]
{{ CompatNo() }} {{ CompatNo() }} {{ CompatNo() }}

Gecko notes

[1] Disabled by default in Firefox 10.0, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11.0, mozBattery is enabled by default.

[2] The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 {{geckoRelease("18.0")}}.

See also

Revision Source

<p>{{ domref() }}</p>
<p>The <code>battery</code> object provides information about the system's battery charge level; you can also listen to events it sends that provide updates about the battery's charge status. This implements the <a href="/en-US/docs/WebAPI/Battery_Status" title="/en-US/docs/WebAPI/Battery_Status">Battery Status API</a>; see that documentation for additional details, a guide to using the API, and sample code.</p>
<h2 id="Attributes">Attributes</h2>
<dl>
  <dt>
    {{domxref("window.navigator.battery.charging", "charging")}} {{ReadOnlyInline}}</dt>
  <dd>
    A Boolean value indicating whether or not the battery is currently being charged.</dd>
  <dt>
    {{domxref("window.navigator.battery.chargingTime", "chargingTime")}} {{ReadOnlyInline}}</dt>
  <dd>
    A number representing the remaining time in seconds until the battery is fully charged, or 0 if the battery is already fully charged.</dd>
  <dt>
    {{domxref("window.navigator.battery.dischargingTime", "dischargingTime")}} {{ReadOnlyInline}}</dt>
  <dd>
    A number representing the remaining time in seconds until the battery is completely discharged and the system will suspend.</dd>
  <dt>
    {{domxref("window.navigator.battery.level", "level")}} {{ReadOnlyInline}}</dt>
  <dd>
    A number representing the system's battery charge level scaled to a value between 0.0 and 1.0.</dd>
</dl>
<h2 id="Events">Events</h2>
<p>You can listen to the <code>battery</code> object to receive these events that let you know when the battery charge state has changed.</p>
<dl>
  <dt>
    <code>chargingchange</code></dt>
  <dd>
    Fires when the battery charging state is updated.</dd>
  <dt>
    <code>chargingtimechange</code></dt>
  <dd>
    Fires when the battery charging time is updated</dd>
  <dt>
    <code>dischargingtimechange</code></dt>
  <dd>
    Fires when the battery discharging time is updated.</dd>
  <dt>
    <code>levelchange</code></dt>
  <dd>
    Fires when the battery level is updated.</dd>
</dl>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Specification</th>
      <th scope="col">Status</th>
      <th scope="col">Comment</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>{{SpecName('Battery API')}}</td>
      <td>{{Spec2('Battery API')}}</td>
      <td>Initial specification.</td>
    </tr>
  </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ Unimplemented_inline_webkit("90538") }}</td>
        <td>{{CompatGeckoDesktop("10")}} {{ property_prefix("moz") }} [1]<br />
          {{CompatGeckoDesktop("16")}} (without prefix) [2]</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Chrome for Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>{{ CompatUnknown() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{CompatGeckoMobile("10")}} {{ property_prefix("moz") }} [1]<br />
          {{CompatGeckoMobile("16")}} (without prefix) [2]</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
        <td>{{ CompatNo() }}</td>
      </tr>
    </tbody>
  </table>
</div>
<h3 id="Gecko_notes">Gecko notes</h3>
<p>[1] Disabled by default in Firefox 10.0, but can be enabled setting the preference <code>dom.battery.enabled</code> to <code>true</code>. Starting with Firefox 11.0, <code>mozBattery</code> is enabled by default.</p>
<p class="note">[2] The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 {{geckoRelease("18.0")}}.</p>
<h2 id="See_also">See also</h2>
<ul>
  <li><a class="external" href="http://hacks.mozilla.org/2012/02/using-the-battery-api-part-of-webapi/">Blog post - Using the Battery API</a></li>
  <li><a class="external" href="http://davidwalsh.name/battery-api" title="http://davidwalsh.name/battery-api">David Walsh on the JavaScript Battery Api</a></li>
  <li><a href="https://github.com/pstadler/battery.js" title="https://github.com/pstadler/battery.js">battery.js - a tiny cross-browser wrapper</a></li>
</ul>
Revert to this revision