html5判断当前浏览器页面选项卡是否被激活
作用
切换到别的选项卡,再切换回来的时候将会提示
<script>
var hiddenProperty = 'hidden' in document ? 'hidden' :
'webkitHidden' in document ? 'webkitHidden' :
'mozHidden' in document ? 'mozHidden' :
null;
var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
var onVisibilityChange = function(){
if (!document[hiddenProperty]) {
console.log('页面非激活');
alert("页面非激活");
}else{
console.log('页面激活')
alert("页面激活");
}
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);
</script>