Associate
- Joined
- 11 Oct 2008
- Posts
- 268
Hey guys. I am not very experienced with JS, so please bear with me.
I read about the battery api, and according to caniuse.com it should be supported by firefox and chrome. However my code does not work in chrome.
this is the code I came up with:
Can anyone see what I have done wrong. Constructive criticism is welcomed as I would love to improve my JS skills.
Here is the full code with css in case anyone wants to see the whole thing working.
Thanks for any tips
I read about the battery api, and according to caniuse.com it should be supported by firefox and chrome. However my code does not work in chrome.
this is the code I came up with:
Code:
<script>
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBattery;
if (battery) {
// Battery API supported
var displayBattery = Math.floor(battery.level * 100);
}
console.log("Battery level: ", Math.floor(battery.level * 100) + "%");
console.log("Device is ", (battery.charging ? "charging" : "discharging"));
console.log(Math.floor(battery.level * 100));
document.addEventListener('DOMContentLoaded', function() {
var infoStatus = document.getElementsByClassName('infoStatus')[0];
var batt = document.getElementsByClassName('batteryInner')[0];
batt.style.width = displayBattery + "%";
if (battery.charging) {
document.getElementById('status').innerHTML = "Charging";
infoStatus.style.color = "#006400";
} else {
document.getElementById('status').innerHTML = "Discharging";
infoStatus.style.color = "#FF0000";
}
})
</script>
Can anyone see what I have done wrong. Constructive criticism is welcomed as I would love to improve my JS skills.
Here is the full code with css in case anyone wants to see the whole thing working.
Thanks for any tips
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Battery API Test</title>
<script>
var battery = navigator.battery || navigator.webkitBattery || navigator.mozBattery || navigator.msBattery;
if (battery) {
// Battery API supported
var displayBattery = Math.floor(battery.level * 100);
}
console.log("Battery level: ", Math.floor(battery.level * 100) + "%");
console.log("Device is ", (battery.charging ? "charging" : "discharging"));
console.log(Math.floor(battery.level * 100));
document.addEventListener('DOMContentLoaded', function() {
var infoStatus = document.getElementsByClassName('infoStatus')[0];
var batt = document.getElementsByClassName('batteryInner')[0];
batt.style.width = displayBattery + "%";
if (battery.charging) {
document.getElementById('status').innerHTML = "Charging";
infoStatus.style.color = "#006400";
} else {
document.getElementById('status').innerHTML = "Discharging";
infoStatus.style.color = "#FF0000";
}
})
</script>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
letter-spacing: 6px;
font-size: 0.8rem;
}
p {
display: inline;
}
.batteryWrapper {
position: fixed;
top: 36px;
left: 20px;
width: 100px;
height: 10px;
background: #696969;
border-radius: 6px;
overflow: hidden;
}
.batteryInner {
position: relative;
height: 100%;
width: 0%;
background: #98FB98;
}
.infoTop {
position: absolute;
top: 10px;
left: 20px;
}
.infoBottom {
position: absolute;
top: 60px;
left: 20px;
}
.infoStatus {
color: #000000;
}
</style>
</head>
<body>
<div class="infoTop">
Battery level:
</div>
<div class="batteryWrapper">
<div class="batteryInner">
</div>
</div>
<div class="infoBottom">
Battery status:
<span class="infoStatus"><p id="status"></p></span>
</div>
</body>
</html>