监听用户离开或返回当前打开的网页

用户返回浏览器中打开的此页面时,显示id="model-vote"块中内容:

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]) {
    $('#model-vote').attr("style","display:block");
}
   if (document[hiddenProperty]) {
    console.log('用户离开当前页面时,执行此方法');
   }
}
document.addEventListener(visibilityChangeEvent, onVisibilityChange);
posted @ 2017-03-21 15:20  静心jx  阅读(600)  评论(0编辑  收藏  举报