Know the Battery Status of your Visitor's Mobile Phone

K
Published in: Javascript

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

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 visitor’s 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.

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

📮  Subscribe to our Email Newsletter for Google tips and tutorials!
Published in: Javascript

Looking for something? Find here!

Meet the Author

Web Geek, Google Developer Expert
A
Amit Agarwal

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. He is the developer of Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

Get in touch

Google Add-ons

Do more with your Gmail and GSuite account
G

We build bespoke solutions that use the capabilities and the features of Google Workspace for automating business processes and driving work productivity.

  1. Mail Merge with Attachments
    Send personalized email to your Google Contact with a Google Sheet and Gmail
  2. Save Emails and Attachments
    Download email messages and file attachments from Gmail to your Google Drive
  3. Google Forms Email Notifications
    Send email notifications to multiple people when a new Google Form is submitted
  4. Document Studio
    Create beautiful pixel perfect documents merging data from Google Sheets and Google Forms
  5. Creator Studio for Google Slides
    Turn your Google Slides presentations into animated GIFs and videos for uploading to YouTube