正文

JavaScript判断是否在当前标签页

不支持 IE 浏览器

//判断是否在当前标签页
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…