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


Picture showing the laptop battery status whether they are charging or not



Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, October 30, 2018

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250