gpt4 book ai didi

javascript - 检查网站是否在前台

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:20:05 25 4
gpt4 key购买 nike

我想检查我的网站是否在前台使用 JavaScript。但是在阅读所有内容之前不要回答,因为你会直接对我说我应该做 this :

$(window)
.focus(function() {
console.log("focus");
})
.blur(function() {
console.log("blur");
});​

但问题是我的页面上有来自第三方的 iframe,如果用户单击 iframe,窗口模糊事件将被触发,但用户实际上仍在我的页面上。我试图通过检查 if document.activeElement is an iframe on blur 来解决这个问题:

$(window)
.focus(function() {
console.log("focus");
})
.blur(function() {
console.log("blur");
if (document.activeElement.tagName.toLowerCase() == "iframe") {
console.log("still focus");
}
});​

这里的问题是,当用户先点击 iframe 然后再点击另一个应用程序时,我无法检查页面是否不再位于前台。

那么当网站中有 iframe 时,如何检查该网站是否在前台?

最佳答案

您应该能够使用 Page Visibility API .

This specification defines a means for site developers toprogrammatically determine the current visibility state of the page inorder to develop power and CPU efficient web applications.

了解更多

例子

关于javascript - 检查网站是否在前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12185750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com