标签:style blog io ar color sp for on div
window有个beforeunload事件,是在离开页面前触发的;大家肯定会很快联想到unload这个事件,二者有的区别还是蛮大的,字面上就很好理解,beforeunload在时间线上在unload之前执行,分解点就是页面卸载时间;
经常会有一些在用户离开页面前执行一些业务的应用场景,这都要用到onbeforeunload事件;比如记录用户停留时长的业务,在GA等页面访问统计的应用中都包含这个:
1 ;(function(){ 2 var startTime = Math.ceil(new Date().getTime()/1000), //单位秒 3 getDuration = function(){ 4 var time = ‘‘, 5 hours = 0, 6 minutes = 0, 7 seconds = 0, 8 endTime = Math.ceil(new Date().getTime()/1000), 9 duration = endTime - startTime; 10 11 hours = Math.floor(duration/3600); //停留小时数 12 minutes = Math.floor(duration%3600/60); //停留分钟数 13 seconds = Math.floor(duration%3600%60); //停留秒数 14 15 time = (hours < 10 ? ‘0‘ + hours : hours) + ‘:‘ + (minutes < 10 ? ‘0‘ + minutes : minutes) + ‘:‘ + (seconds < 10 ? ‘0‘ + seconds : seconds); 16 17 return time; 18 }; 19 20 21 window.onbeforeunload = function(e){ 22 var duration = getDuration(); 23 24 //request(duration); 25 }; 26 })();
标签:style blog io ar color sp for on div
原文地址:http://www.cnblogs.com/kinnjee/p/4165958.html