Articles → JavaScript → Check If Battery Of Your Laptop Is Charging Or Not Using Battery Status API In Javascript
Check If Battery Of Your Laptop Is Charging Or Not Using Battery Status API In Javascript
Navigator.Getbattery()
Onchargingchange Event
Example
<!DOCTYPE html>
<html>
<head>
<title>Battery Status API Example</title>
<script>
window.onload = function() {
navigator.getBattery().then(function(battery) {
// Update the battery status initially when the promise resolves ...
document.getElementById('charging').textContent = battery.charging ? 'charging' : 'not charging';
// .. and for any subsequent updates.
battery.onchargingchange = function() {
document.getElementById('charging').textContent = battery.charging ? 'charging' : 'not charging';
};
});
};
</script>
</head>
<body>
<div id="charging">(charging state unknown)</div>
</body>
</html>
Output