js 实现浏览器下标签页间切换触发的事件

 

需求:浏览器标签页切换,监听事件

<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('页面非激活');
        }else{
            console.log('页面激活')
            /*document.location.reload();//重新加载当前页面*/
        }
    };
    document.addEventListener(visibilityChangeEvent, onVisibilityChange);

    window.localStorage.setItem("fwbl_prePage","1");
    window.localStorage.setItem("money_use","1");
</script>

 

参考地址:https://blog.csdn.net/cczhumin/article/details/50736825

上一篇:前端基础面试题目(1)


下一篇:【备忘】css文本超出后显示省略号(支持单行、多行)