
正文
js_页面关闭beforeunload事件
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
做圆桌爆文公众号的时候,需要对阅读的文章进行时间统计。是这个公众号的核心功能,客户把文章转发到朋友圈或者转给朋友,记录谁阅读此文章和阅读时长进行记录,从而展示给客户。
功能点是,关闭页面时触发事件,请求后台,把阅读时间记录下来。
技术段:
//原生写法
window.onbeforeunload = function(event) {
return confirm("确定退出吗");
} //JQ 写法
$(window).bind('beforeunload', function() {
return '您输入的内容尚未保存,确定离开此页面吗?';
});
//这里暂停
$(window).unbind('beforeunload');
全部代码:
<!DOCTYPE html>
<html> <head>
<meta charset="UTF-8">
<title></title>
<script src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
</head> <body>
</body>
<script type="text/javascript">
//原生写法
window.onbeforeunload = function(event) {
return confirm("确定退出吗");
} //JQ 写法
$(window).bind('beforeunload', function() {
return '您输入的内容尚未保存,确定离开此页面吗?';
});
//这里暂停
$(window).unbind('beforeunload');
</script> </html>







