js - 触发页面不可见(用于微信非公众号的分享)

访问量: 215

        	var hiddenProperty = 'hidden' in document ? 'hidden' : 'webkitHidden' in document ? 'webkitHidden' : 'mozHidden' in document ? 'mozHidden' : null;
      		var visibilityChangeEvent = hiddenProperty.replace(/hidden/i, 'visibilitychange');
      		var time = 0;
      		var url = _sharedata[0].link;
          history.pushState({url: url, title: document.title}, document.title, url)
          var onVisibilityChange = function(){
      			if (document[hiddenProperty]) {
                                alert('== 触发了一次不可见')
      				time += 1
      				//var url = location.href.replace(/#\d+/, "&tt=jump")
      				//var url = location.href.replace(/#\d+/, "&jump=" + escape(_sharedata[time].link))
      				//var url = location.href + "&jump=" + escape(_sharedata[time].link);
      				var url = _sharedata[time].link;
            	history.pushState({url: url, title: document.title}, document.title, url)
      			} else {
      				time += 1
      				_share_list(time)
      			}
      		}
      		document.addEventListener(visibilityChangeEvent, onVisibilityChange);

以上是部分代码, 在PC端一旦切换浏览器窗口,机会弹出alert. 

在手机端,则是: 微信分享时, 会检验用户离开了当前页面。 判定是在分享

订阅/RSS Feed

Subscribe

分类/category