t just static files. There is increased client-side processing in today
’
s rich web apps. This has resulted in increasing power consumption by browsers. As internet gets increasingly consumed through mobile devices, it is important for web developers to design their web pages with the consideration that the device rendering the HTML might be power constrained.
If the web application can be made aware of the battery status of the device, the web developers can author the web application such that it can stop or reduce the power-intensive processing if the device is low on battery.
With this scenario in mind, the HTML5 specification has provided API for determining the battery status. With the knowledge of the device’s battery status, the web application can effectively craft the content and optimized experience.
The BatteryManager interface is specified as below.
[NoInterfaceObject]
interfaceBatteryManager : EventTarget {
readonly attribute boolean charging;
readonlyattributedouble chargingTime;
readonlyattributedouble dischargingTime;
readonlyattributedouble level;
[TreatNonCallableAsNull]
attributeFunction? onchargingchange;
[TreatNonCallableAsNull]
attributeFunction? onchargingtimechange;
[TreatNonCallableAsNull]
attributeFunction? ondischargingtimechange;
[TreatNonCallableAsNull]
attributeFunction? onlevelchange;
};
The BatteryManager interface exposes 4 properties
charging – boolean – represents if the system’s battery is charging
chargingTime – double – represents the time remaining (in seconds) for the battery to be fully charged.
dischargingTime – double – represents the time remaining (in seconds) for the battery to be completely discharged and the system is about to be suspended.
level – double – represents the current battery level, min 0.0, max 1.0.
It also specifies 4 events.
onchargingchange – Event fired when charging activity changes.
onchargingtimechange –Event fired when the time remaining to completely charge the device changes.
ondischargingtimechange –Event fired when the time remaining to completely discharge the device changes.
onlevelchange – Event fired when the level changes.
NavigatorBattery
The HTML5 specification outlines that all kinds of Navigator type implement the NavigatorBattery interface, which is defined as below.
This means that access to the battery is made by calling “navigator.battery”.
Advertisement
Hands On
Here is a snippet which shows how we can use the battery API in HTML5 web pages. Note that the API is not currently implemented by any modern browser, so these samples might not work.
<!DOCTYPE html>
<html>
<head>
<!—->
</head>
<body>
<header>
<h1>Battery API demo</h1>
<p>Demo showing battery API in action (does not work on all browsers)</p>
</header>
<footer>
<h1></h1>
<p>HTML Goodies</p>
</footer>
<div id=”charging”>(charging state unknown)</div>
<div id=”level”>(battery level unknown)</div>
<div id=”chargingTime“>(charging time unknown)</div>
<div id=”dischargingTime“>(discharging time unknown)</div>
<script>
// Get the battery object
var battery = navigator.battery;
//Trap the charging change event and update the element “charging” with whether the device is charging or not.
If the browser supported battery API, it will show the battery information.
Some browsers have implemented the support for battery API differently. To work with this, you can declare the battery object as below as use it today.
In this article, we learned about the battery API. I hope you have found this information useful.
About the author
Vipul Patel is a Program Manager currently working at Amazon Corporation. He has formerly worked at Microsoft in the Lync team and in the .NET team (in the Base Class libraries and the Debugging and Profiling team). He can be reached at vipul.patel@hotmail.com
The original home of HTML tutorials. HTMLGoodies is a website dedicated to publishing tutorials that cover every aspect of being a web developer. We cover programming and web development tutorials on languages and technologies such as HTML, JavaScript, and CSS. In addition, our articles cover web frameworks like Angular and React.JS, as well as popular Content Management Systems (CMS) that include WordPress, Drupal, and Joomla. Website development platforms like Shopify, Squarespace, and Wix are also featured. Topics related to solid web design and Internet Marketing also find a home on HTMLGoodies, as we discuss UX/UI Design, Search Engine Optimization (SEO), and web dev best practices.
Advertiser Disclosure: Some of the products that appear on
this site are from companies from which TechnologyAdvice
receives compensation. This compensation may impact how and
where products appear on this site including, for example,
the order in which they appear. TechnologyAdvice does not
include all companies or all types of products available in
the marketplace.