Is there a reliable way to check from an open webpage with javascript if a laptop has been suspended (laptop lid closed/opened)?
-
10No. You can monitor key presses and mouse moves though to check for user activity. Also see the `PageVisibility` [HTML5 API](http://www.html5rocks.com/en/tutorials/pagevisibility/intro/) – Matt Jul 28 '13 at 22:54
-
3why do you wan't to do this – iConnor Jul 28 '13 at 22:58
-
[Discussion of this question can be found here](http://meta.stackexchange.com/questions/191129/how-could-i-have-avoided-this-question-being-put-on-hold) – Andrew Barber Jul 31 '13 at 00:15
-
I've marked this as a duplicate, instead. Although the target question uses the word "Desktop", it simply means that as opposed to a mobile device. – Andrew Barber Jul 31 '13 at 02:05
3 Answers
I have managed to achieve this by using a javascript interval that checks how much time has passed. If the power is suspended then the time difference between ticks is off.
<script>
var last = (new Date()).getTime();
setInterval(function(){
var current = (new Date()).getTime();
if (current-last > 3000) {
console.log('power was suspended');
}
last = current;
}, 1000);
</script>

- 1,191
- 1
- 13
- 24
-
2Incidentally, that's not the same thing your question asked for; perhaps you just worded the question differently than you meant? Your question seemed to be asking to detect if power **is** suspended, not **was**. – Andrew Barber Jul 31 '13 at 00:48
-
I think I'll go with this. I have a web page doing a long-poll to a server, and it seems screwed up after I suspend/resume the PC (connection lingers, but should have been closed). Need to evaluate this more, but I think that's what happens. – Stefan Reich Apr 05 '20 at 09:38
Simply no. These are the kind of things that JavaScript isn't exposed to, for security reasons :). Same with the file system, although the File API gives some functionality.
It may also seem like this feature will lack usefulness, however, if it existed the website could do stuff "while you're not watching" which I don't believe is ever a good thing :)
The BatteryAPI as a few people mentioned in the comments is extremely important for some websites. For example, to know whether or not to run resource-consuming animations or functions, save all user data before he loses his work, etc. (especially when running on smartphones and tablets… not only laptops).

- 218,210
- 55
- 464
- 476

- 15,310
- 11
- 62
- 65
-
2
-
Yes i think you should explain why because, there is a battery api? right and closing the lid is slightly to do with battery, ie: standby. soo... – iConnor Jul 28 '13 at 22:57
-
-
I don't think it has much to do with security, maybe privacy. And that W3C/WHATWG didn't bother making an API for that (yet). – Fabrício Matté Jul 28 '13 at 22:58
-
Allowing JavaScript to know such information is not responsible. Since JavaScript is a client-side language (node.js excluded), it is a security issue mainly. – ronnied Jul 28 '13 at 22:58
-
@Matt well i have heard of it http://www.w3.org/TR/battery-status/ never looked into it though.. – iConnor Jul 28 '13 at 22:59
-
This isn't strictly true. http://stackoverflow.com/questions/17913785/how-can-i-detect-if-the-laptop-lid-is-closed-in-javascript/17913821#17913821 – Nate Higgins Jul 28 '13 at 23:00
-
@Connor: Heh, from that you get [here](http://www.w3.org/TR/system-info-api/#idl-def-Display), which gives you whether the screen is "blanked" or not, so I guess the *actual* answer to the OP is "yes you can" (although I don't know any devices which support this :P). – Matt Jul 28 '13 at 23:01
-
1In JS you can access the device's camera and microphone as long as the user gives permission to, "security" is hardly an excuse nowadays. Lack of usefulness to make an API solely for that I'd say. – Fabrício Matté Jul 28 '13 at 23:01
-
@FabrícioMatté I agree with the lack of usfulness.. However someone can use it to do stuff while "you're not watching" like opening and closing browser windows to etc.. – Yotam Omer Jul 28 '13 at 23:04
-
Okay, +1 for that point which I've overlooked. Even then, such an API would most likely have to request user permission first as the UserMedia API do. `=]` – Fabrício Matté Jul 28 '13 at 23:06
-
-
You say "simply, no", but there is an answer on this page demonstrating exactly how to do this. – Stefan Reich Apr 05 '20 at 09:37
@yotam-omer's answer isn't strictly true. While you can't detect the physical action of shutting the lid, you can detect leaving the page, and using polling to detect a loss of connection (i.e., shutting the lid). Pairing the two can be used to detect a loss of connection that isn't leaving the page.
If this sounds interesting, comment and I'll add an example.

- 2,104
- 16
- 21
-
This is the kind of thing i'm thinking of, I realise there isn't actually a browser API for whether the lid is closed ;-) – Tom Jul 28 '13 at 23:00
-
3shutting the lid not always goes to sleep or hibernation.. that's user's preferences.. so there will be no loss of connection – Yotam Omer Jul 28 '13 at 23:00
-
1Well, shutting the lid would be irrelevant to the application if it actually did nothing... – Nate Higgins Jul 28 '13 at 23:01
-
also if the computer is asleep its irrelevant to the application :) – Yotam Omer Jul 28 '13 at 23:01
-
1
-
When you leave the page no javascript is running, so I don't understand your suggestion. How can JS be used to detect when it itself is no longer running? – Asad Saeeduddin Jul 31 '13 at 02:00