Question: **/!\ THE USE OF window.navigator.battery [IS STRONGLY DISCOURAGED](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/battery#Quick_Links) AND THIS ISSUE IS NOW NOT WORTH CHECKING THANK YOU /!\**

I want to get the battery level of the current system. Here is the output I intend to get:

**If desktop (no battery)**
Battery level : 100%

**If laptop**
Battery level : XXX% (depending the level)

So I tried the snippet of the Mozilla Foundation over [`window.navigator.battery`](https://developer.mozilla.org/en/docs/Web/API/Navigator/battery) in my **[JSFiddle](https://jsfiddle.net/)**.

**The problem :** Running Chrome 20+ version (currently 46), I still have an error on the console :

> 
> Uncaught TypeError: Cannot read property 'addEventListener' of undefined
> 
> 
> 

So what I understand is the object `battery` instantiated with the supposed battery level is returning nothing.

Did someone figured this out already ?

*The point of my algorithm when this snippet works is to define which action the user cannot perform regarding the remaining battery level. For example, I don't want the user to engage a heavy action that could probably lower his battery level to a critical point and make the critical action fail.*

Answer: 
From the [English version of that page](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/battery):

> 
> This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
> 
> 
> 

and 

> 
> The `battery` property is deprecated and has been replaced by a [`Navigator.getBattery()`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery "The getBattery() method provides information about the system's battery, returns a battery promise, which is resolved in a BatteryManager object providing also some new 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.") method returning a battery [`Promise`](https://developer.mozilla.org/en-US/docs/Web/API/Promise "The Promise interface represents a proxy for a value not necessarily known at its creation time. It allows you to associate handlers to an asynchronous action's eventual success or failure. This lets asynchronous methods return values like synchronous methods: instead of the final value, the asynchronous method returns a promise of having a value at some point in the future."). Its support is still partial though.
> 
> 
>
**WARNING**

Do not use the following answer in production, as it has been [deprecated since then](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/getBattery). The spec will be reworked due to [privacy and security issues](https://lukaszolejnik.com/AssessingPrivacyWebStandardsIWPE17.pdf), so expect to see changes and malfunctions.

**ANSWER**

To answer my own question, thanks to the help of the document that **@Quentin** provided to me, I reached my goal using `BatteryManager.level` function the the promise `Navigator.getBattery()`. The link that gives the working snippet can be found here : <https://developer.mozilla.org/en-US/docs/Web/API/BatteryManager/level>.

The following JSFiddle display in the console (F12) the value of the remaining battery (I could not verify the ouput in a desktop yet, so please correct me if it is not working on desktop) :

**[JSFiddle](https://jsfiddle.net/1km5tsc4/)**

**JavaScript**

```
navigator.getBattery().then(function(battery) {

    var level = battery.level;

    console.log(level);
});

```