avatar

目录
JavaScript常用效果

正文

JavaScript判断是否在当前标签页

不支持 IE 浏览器

Javascript
//判断是否在当前标签页
let hidden, state, visibilityChange;
if (typeof document.hidden !== "undefined") {
hidden = "hidden";
visibilityChange = "visibilitychange";
state = "visibilityState";
} else if (typeof document.mozHidden !== "undefined") {
hidden = "mozHidden";
visibilityChange = "mozvisibilitychange";
state = "mozVisibilityState";
} else if (typeof document.msHidden !== "undefined") {
hidden = "msHidden";
visibilityChange = "msvisibilitychange";
state = "msVisibilityState";
} else if (typeof document.webkitHidden !== "undefined") {
hidden = "webkitHidden";
visibilityChange = "webkitvisibilitychange";
state = "webkitVisibilityState";
}
// 添加监听器,在title里显示状态变化
document.addEventListener(
visibilityChange,
function() {
if (document[state] === "visible") {
document.title = "回来啦!";
} else {
document.title = "去哪了?";
}
},
false
);
//初始化页面状态
document.title = "回来啦!";

to be continued…

文章作者: Tamsiree
文章链接: https://tamsiree.com/TechnicalResearch/JavaScript/JavaScript%E5%B8%B8%E7%94%A8%E6%95%88%E6%9E%9C/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Tamsiree
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论