js判断是否在当前页面
2019-06-19 本文已影响0人
干炸里脊不如你
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>欢迎回来</title>
</head>
<body>
<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(22)
document.title = '欢迎回来';
}else{
console.log('不在当前页面')
document.title = '你为什么要离开我';
}
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);
</script>
</body>
</html>