HTML, Battery Level |
HTML, Battery Level |
s1lv3rf0x87 |
Jan 18 2017, 06:38 AM
Post
#1
|
Group: Members Posts: 5 Joined: 18-January 17 Member No.: 26,270 |
Hey Guys,
I have some HTML code which will display the battery status of a device and display the charge as a decimal value. I am trying to tweak the code so it displays the charge value as a percentage and not a decimal value. Can someone have a look at this and advise if this is possible and if so how to do this. Many Thanks in advance. CODE <html> <head> <title>Battery Status API Example</title> <script> window.onload = function () { function updateBatteryStatus(battery) { document.querySelector('#charging').textContent = battery.charging ? 'charging' : 'not charging'; document.querySelector('#level').textContent = battery.level; document.querySelector('#dischargingTime').textContent = battery.dischargingTime / 60; } navigator.getBattery().then(function(battery) { // Update the battery status initially when the promise resolves ... updateBatteryStatus(battery); // .. and for any subsequent updates. battery.onchargingchange = function () { updateBatteryStatus(battery); }; battery.onlevelchange = function () { updateBatteryStatus(battery); }; battery.ondischargingtimechange = function () { updateBatteryStatus(battery); }; }); }; </script> </head> <body> <div id="charging">(charging state unknown)</div> <div id="level">(battery level unknown)</div> <div id="dischargingTime">(discharging time unknown)</div> </body> </html> |
Christian J |
Jan 18 2017, 08:09 AM
Post
#2
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
Note that browser support still seems very limited:
http://caniuse.com/#search=battery https://developer.mozilla.org/en/docs/Web/A...r_compatibility |
pandy |
Jan 18 2017, 08:23 AM
Post
#3
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Jesus, it's a JavaScript method! I didn't know and couldn't figure out how the script worked.
|
pandy |
Jan 18 2017, 08:28 AM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
The sample at the top of the page at CanIUse must be hardcoded. It says the same either I use my desktop or my phone. I tried to run the script locally on my phone and it didn't work.
|
Christian J |
Jan 18 2017, 03:33 PM
Post
#5
|
. Group: WDG Moderators Posts: 9,656 Joined: 10-August 06 Member No.: 7 |
|
pandy |
Jan 18 2017, 04:30 PM
Post
#6
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Yeah, but I don't get a choice of browser. It opens in something called HTML Viewer IIRC. But I tried over a server too. So no, I don't think my phone supports this.
|
pandy |
Jan 18 2017, 04:42 PM
Post
#7
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,730 Joined: 9-August 06 Member No.: 6 |
Oh, you asked about JS. I thought you said HTML. Well, I thought so. After all it runs on the phone when the page is on the web too, so why not? But it looks like I thought wrong. I uploaded a page with just 'document.write('Hello!');' in it to my phone and it loads blank.
|
Lo-Fi Version | Time is now: 24th April 2024 - 07:34 AM |