Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript should run only if tab/browser window is focused [duplicate]

Possible Duplicate:
Detect If Browser Tab Has Focus

I have a simple java applet that captures client's screen. With the help of a piece of javascript code, I am able to call the applet and capture the active screen picture.

But, even though it only captures active screen with a click on a button, users are likely to manipulate the process by switching to some other tab with ALT TAB while capture process. I want to make sure that capturing must be done only if page is loaded and page is focused.

So far, I found this piece of javascript code which doesn't seem to be working correctly. Sometimes it gets stuck at the focus even though the page is minimized.

<script language="javascript">
     window.onpageshow = function(e) { console.log('pageshow'); };
     window.onfocus = function(e) { console.log('focus'); };
</script>

So are there any suggestions to what I am trying to achieve or any other solutions.

like image 403
Cute Bear Avatar asked Dec 19 '12 17:12

Cute Bear


1 Answers

Try using document.hidden it's part of the page visibility API

like image 84
Mottie Avatar answered Nov 04 '22 02:11

Mottie