只有在选项卡/浏览器窗口聚焦时,javascript才能运行 [英] javascript should run only if tab/browser window is focused

查看:186
本文介绍了只有在选项卡/浏览器窗口聚焦时,javascript才能运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

检测浏览器选项卡是否具有焦点

我有一个简单的Java小程序,可以捕获客户端的屏幕。借助一段javascript代码,我可以调用applet并捕获活动的屏幕图片。

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.

但是,即使它只捕获活动屏幕单击按钮,用户可能会在捕获过程中通过ALT TAB切换到其他选项卡来操纵该过程。我想确保只有在加载页面并且页面被聚焦时才能进行捕获。

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.

到目前为止,我发现这段javascript代码似乎没有工作正常。即使页面被最小化,有时也会卡在焦点上。

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.

推荐答案

尝试使用 document.hidden 它是页面可见性API

这篇关于只有在选项卡/浏览器窗口聚焦时,javascript才能运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆