Sunday, 22 March 2015

// // Leave a Comment

Best Way to Know the Battery Status of your Visitor’s Mobile Phone

Share
When someone comes to your website or your Blog, you can easily view information about the charge level of their mobile or laptop’s battery through the Battery Status API . It is currently supported on Google Chrome, Opera & Firefox on the desktop and according to me only Chrome for Android.


The Battery API can be implemented with few lines of JavaScript code and reveals all the required details about the device’s battery charge level. You’ll get to know:
  1. Whether or not the visitor’s battery is currently being charged.
  2. How much is the battery charged?
  3. If charging, how many seconds until the battery is fully charged.
  4. The remaining time in seconds until the battery is completely discharged.
    You can attach event listeners so the battery data is updated as soon as the charge level of the hardware’s battery is changed while the visitor is still on your page. You can go one step further and even integrate this with Google Analytics and store the battery charge level of your visitor’s devices using Events in Analytics.
    Use This-
    <script>

    if (navigator.getBattery) {
    navigator.getBattery().then(function(battery) {
    display(battery);
    });
    } else if (navigator.battery) {
    display(navigator.battery);
    } else {
    console.log("Sorry, Battery Status API is not supported");
    }

    function display(battery) {
    console.log('Charge level? ' + battery.level);
    console.log('Battery charging? ' + battery.charging);
    console.log('Time to charge? ' + battery.chargingTime);
    console.log('Time to discarge? ' + battery.dischargingTime);
    }

    </script>
      This can have several use cases. For instance, when the visitor’s device is running low on battery and not plugged-in, the web developer can choose to automatically save the changes – like the form entries – in local Storage before the battery is completely drained.
      Here’s a complete list of browsers that currently support the Batter Status API as found on caniuse.com. To know more, refer to the documentation on Mozilla and W3.
Author

Admin Of Blog: Paramjot Singh

I am Paramjot Singh, a young addicted Blogger. I am currently 21 years old and living in Lucknow, India. I love to play with codes and share my technical resources and will continue to add my two cents that might be helpful in your blogging journey. :)


Stay connected, You'll not be disappointed!

"Be The First To Comment Here"

Hii..
Everyone..!!!
I am Very Happy If you Comment here and ask me your Questions related to the topic.
NOTE : COMMENT HERE WITH USING YOUR NAME