API Battery Status

Obsolète
Cette fonctionnalité est obsolète. Bien qu'encore supportée par des navigateurs, son utilisation est découragée pour tout nouveau projet. Évitez de l'utiliser.

L'API Battery Status, souvent mentionnée sous le nom Battery API, fournit des informations sur le niveau de charge du système et permet d'envoyer des événements pour prévenir d'un changement du niveau de charge de la batterie. Cela peut être utilisé pour ajuster la consommation d'une application et la réduire pour réduire l'utilisation de la batterie lorsque son niveau de charge est bas ou encore de sauvegarder les données quand la batterie est bientôt vide.

L'API Battery Status API étend l'interface window.navigator avec la propriété window.navigator.battery, méthode qui renvoie une promesse, résolue par l'objet BatteryManager. Cet objet permet d'ajouter des nouveaux événements pour superviser l'état de la batterie.

Exemples

Dans cet exemple, on cherche à surveiller les changements, à la fois du statut de la charge (est-ce que l'équipement est branché et se recharge ou est ce que l'équipement est alimenté par sa batterie) et des changements du niveau de charge dans le temps.

Cela se fait en surveillant les événements : 

navigator.getBattery().then(function(battery) {
  function updateAllBatteryInfo(){
    updateChargeInfo();
    updateLevelInfo();
    updateChargingInfo();
    updateDischargingInfo();
  }
  updateAllBatteryInfo();

  battery.addEventListener('chargingchange', function(){
    updateChargeInfo();
  });
  function updateChargeInfo(){
    console.log("Battery en charge ? "
                + (battery.charging ? "Oui" : "Non"));
  }

  battery.addEventListener('levelchange', function(){
    updateLevelInfo();
  });
  function updateLevelInfo(){
    console.log("Niveau de batterie: "
                + battery.level * 100 + "%");
  }

  battery.addEventListener('chargingtimechange', function(){
    updateChargingInfo();
  });
  function updateChargingInfo(){
    console.log("Temps avant charge de la batterie: "
                 + battery.chargingTime + " secondes");
  }

  battery.addEventListener('dischargingtimechange', function(){
    updateDischargingInfo();
  });
  function updateDischargingInfo(){
    console.log("Autonomie sur batterie: "
                 + battery.dischargingTime + " secondes");
  }

});

Voir aussi l'exemple de la spécification.

Spécifications

Spécification État Commentaires
Battery Status API Candidat au statut de recommandation Définition initiale.

Compatibilité des navigateurs

Update compatibility data on GitHub
OrdinateurMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariWebview AndroidChrome pour AndroidFirefox pour AndroidOpera pour AndroidSafari sur iOSSamsung Internet
BatteryManager
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
charging
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
chargingTime
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
dischargingTime
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
level
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
onchargingchange
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
onchargingtimechange
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
ondischargingtimechange
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui
onlevelchange
Obsolète
Chrome Support complet 38Edge Aucun support NonFirefox Aucun support 16 — 52
Aucun support 16 — 52
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Support complet 52
Notes Désactivée
Notes Disabled by default in Firefox 10, but can be enabled setting the preference dom.battery.enabled to true. Starting with Firefox 11, mozBattery is enabled by default. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Firefox 18. Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 52: this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
IE Aucun support NonOpera Support complet 25Safari Aucun support NonWebView Android Support complet 40Chrome Android Support complet 38
Notes
Support complet 38
Notes
Notes Values for BatteryManager.chargingTime and BatteryManager.dischargingTime are always equal to Infinity.
Firefox Android Aucun support 10 — 16
Préfixée
Aucun support 10 — 16
Préfixée
Préfixée Nécessite l'utilisation d'un préfixe : moz
Aucun support 16 — 52
Notes Désactivée
Notes 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. The Battery API is currently supported on Android, Windows, and Linux with UPower installed. Support for MacOS is available starting with Gecko 18.0 (Firefox 18.0 / Thunderbird 18.0 / SeaMonkey 2.15). Firefox also provide support for the deprecated navigator.battery.
Désactivée From version 16 until version 52 (exclusive): this feature is behind the dom.battery.enabled preference. To change preferences in Firefox, visit about:config.
Support complet 52
Notes
Notes From this version onwards, the Battery Status API is only available in chrome/privileged code.
Opera Android Support complet 25Safari iOS Aucun support ? — ?Samsung Internet Android Support complet Oui

Légende

Support complet  
Support complet
Aucun support  
Aucun support
Obsolète. Les nouveaux sites web ne doivent pas utiliser cette fonctionnalité.
Obsolète. Les nouveaux sites web ne doivent pas utiliser cette fonctionnalité.
Voir les notes d'implémentation.
Voir les notes d'implémentation.
Une action explicite de l'utilisateur est nécessaire pour activer cette fonctionnalité.
Une action explicite de l'utilisateur est nécessaire pour activer cette fonctionnalité.
Cette fonctionnalité nécessite un préfixe particulier ou utilise un autre nom.
Cette fonctionnalité nécessite un préfixe particulier ou utilise un autre nom.

Voir aussi