Calculate the Battery Level and Charging Status with HTML5

When someone visits your website, you can easily retrieve information about the charge level of their mobile or laptop’s battery through the HTML5 Battery API. This is currently supported on Google Chrome, Opera & Firefox on the desktop and Chrome for Android.

Companies like Uber look at the battery status of their customer’s mobile phone and may apply a price surge if the battery is low because of the urgency of the customer.

The Battery API can be implemented with few lines of JavaScript code and reveals all the required details about the device’s battery charge level. You’ll get to know:

  1. Whether or not the battery is currently being charged.
  2. How much is the battery charged?
  3. If charging, how many seconds until the battery is fully charged.
  4. The remaining time in seconds until the battery is completely discharged.

Battery Status Demo

You can attach event listeners so the battery data is updated as soon as the charge level of the hardware’s battery is changed while the visitor is still on your page. You can go one step further and even integrate this with Google Analytics and store the battery charge level of your visitor’s devices using Events in Analytics.

<script>
  if (navigator.getBattery) {
    navigator.getBattery().then(function (battery) {
      display(battery);
    });
  } else if (navigator.battery) {
    display(navigator.battery);
  } else {
    console.log('Sorry, Battery Status API is not supported');
  }

  function display(battery) {
    console.log('Charge level? ' + battery.level);
    console.log('Battery charging? ' + battery.charging);
    console.log('Time to charge? ' + battery.chargingTime);
    console.log('Time to discarge? ' + battery.dischargingTime);
  }
</script>

This can have several use cases. For instance, when the visitor’s device is running low on battery and not plugged-in, the web developer can choose to automatically save the changes - like the form entries - in localStorage before the battery is completely drained.

<!-- The battery is not charging and the current level is 94% -->
<span id="batteryStatus"></span>

<script>
  if ('getBattery' in navigator) {
    navigator.getBattery().then((battery) => {
      const { level, charging } = battery;
      const status = charging ? 'charging' : 'not charging';
      const percent = `${Math.round(level * 100)}%`;
      const message = `The battery is ${status} and the current level is ${percent}`;
      document.getElementById('batteryStatus').textContent = message;
    });
  }
</script>

Here’s a complete list of browsers that currently support the Batter Status API as found on caniuse.com. To know more, refer to the documentation on Mozilla and W3.

HTML5 Battery Status

Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.