- Navigator object provides Battery API for accessing charging attribute.
- a BatteryManager object can be accessed using getBattery() method.
- In this demo, “We will learn to access charging attribute on chargingchange event fired by BatteryManager”.
- The following code shows the accessing of BatteryManager API.
<!DOCTYPE html>
<html>
<head lang="en">
<title>Battery Demo</title>
</head>
<body>
<script>
navigator.getBattery().then(function(battery) {
console.log(battery);
console.log("Battery charging :", battery.charging );
battery.addEventListener('chargingchange', function() {
console.log("Battery charging :", battery.charging );
});
});
</script>
</body>
</html>
- The demo code is embedded in below JSBIN link. Battery Demo
- when you remove the Power plug the charging attribute becomes false.The following screenshot shows the value of charging attribute.
