Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect whether client system is Laptop or Desktop using JavaScript

Tags:

javascript

I want to show the battery status of client's system and a clock in a widget in my webpage If it is a Laptop.
If it is a desktop, I don't want to show the battery status.
The clock widget is working fine.

Also I am able to get the battery details by using navigator.getBattery().
But if it is Desktop, I don't want to show the widget.

So, How to detect whether the client using Desktop or Laptop using JavaScript?

The below is the contents of navigator but it didn't have details to detect whether it is a Laptop or Desktop.

console.log(navigator);
{
  "vendorSub": "",
  "productSub": "20030107",
  "vendor": "Google Inc.",
  "maxTouchPoints": 0,
  "hardwareConcurrency": 4,
  "appCodeName": "Mozilla",
  "appName": "Netscape",
  "appVersion": "5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
  "platform": "Win32",
  "product": "Gecko",
  "userAgent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
  "language": "en-GB",
  "languages": [
    "en-US",
    "en"
  ],
  "onLine": true,
  "cookieEnabled": true,
  "doNotTrack": null,
  "geolocation": {
    "getCurrentPosition": function getCurrentPosition() { [native code] },
    "watchPosition": function watchPosition() { [native code] },
    "clearWatch": function clearWatch() { [native code] }
  },
  "mediaDevices": {
    "enumerateDevices": function enumerateDevices() { [native code] },
    "getSupportedConstraints": function getSupportedConstraints() { [native code] },
    "getUserMedia": function getUserMedia() { [native code] },
    "addEventListener": function addEventListener() { [native code] },
    "removeEventListener": function removeEventListener() { [native code] },
    "dispatchEvent": function dispatchEvent() { [native code] }
  },
  "plugins": {
    "0": [object Plugin],
    "1": [object Plugin],
    "2": [object Plugin],
    "3": [object Plugin],
    "4": [object Plugin],
    "length": 5,
    "item": function item() { [native code] },
    "namedItem": function namedItem() { [native code] },
    "refresh": function refresh() { [native code] }
  },
  "mimeTypes": {
    "0": [object MimeType],
    "1": [object MimeType],
    "2": [object MimeType],
    "3": [object MimeType],
    "4": [object MimeType],
    "5": [object MimeType],
    "6": [object MimeType],
    "length": 7,
    "item": function item() { [native code] },
    "namedItem": function namedItem() { [native code] }
  },
  "webkitTemporaryStorage": {
    "queryUsageAndQuota": /**id:16**/ function queryUsageAndQuota() { [native code] },
    "requestQuota": /**id:17**/ function requestQuota() { [native code] }
  },
  "webkitPersistentStorage": {
    "queryUsageAndQuota": /**ref:16**/,
    "requestQuota": /**ref:17**/
  },
  "serviceWorker": /**error accessing property**/,
  "getBattery": function getBattery() { [native code] },
  "sendBeacon": function sendBeacon() { [native code] },
  "requestMediaKeySystemAccess": function requestMediaKeySystemAccess() { [native code] },
  "getGamepads": function getGamepads() { [native code] },
  "webkitGetUserMedia": function webkitGetUserMedia() { [native code] },
  "javaEnabled": function javaEnabled() { [native code] },
  "vibrate": function vibrate() { [native code] },
  "requestMIDIAccess": function requestMIDIAccess() { [native code] },
  "credentials": {
    "get": function get() { [native code] },
    "store": function store() { [native code] },
    "requireUserMediation": function requireUserMediation() { [native code] }
  },
  "storage": {
    "persisted": function persisted() { [native code] },
    "persist": function () { [native code] }
  },
  "permissions": {
    "query": function query() { [native code] }
  },
  "presentation": {
    "defaultRequest": null
  },
  "getUserMedia": function getUserMedia() { [native code] },
  "registerProtocolHandler": function registerProtocolHandler() { [native code] },
  "unregisterProtocolHandler": function unregisterProtocolHandler() { [native code] }
}
like image 479
Sagar V Avatar asked Mar 04 '17 13:03

Sagar V


People also ask

How can you determine the client OS in JavaScript?

To detect the client machine's operating system, we can use the navigator object provided by JavaScript. The navigator object provides us with methods such as navigator. appVersion & navigator. userAgent to help us achieve our goal.

How do I check if a website is open in mobile or desktop JS?

We can use the CSS media queries to check whether the website is opened inside a web browser or a mobile browser. This information can be fetched using the min-width and the max-width of the webpage.

How do I know my request is coming from mobile or PC?

you can use below code, var index = navigator. appVersion. indexOf("Mobile");


1 Answers

As mentioned in comments - it's possible to get charging status (true/false) battery.charging and charging time (in seconds) battery.chargingTime from the Battery API

On a desktop the charging is always true but charging time is always 0

This allows us to determine it's a desktop

Here's a quick snippet to test if desktop...

navigator.getBattery().then(function(battery) {
    if (battery.charging && battery.chargingTime === 0) {
        console.log("I'm a desktop")
    } else {
        console.log("I'm not a desktop")
    }
});

Note

This is not an exact science. As @MatheusAvellar pointed out. My guess is if you're fully charged although battery.charging may be true, battery.chargingTime may then go to 0 - don't have access to laptop to try

like image 135
StudioTime Avatar answered Sep 30 '22 16:09

StudioTime