浏览器右键、刷新、默认事件、打开新窗口、关闭续

onbeforeunload和onunload都是在刷新和关闭浏览器事触发。
onbeforeunload在onunload之前触发,因为有before,开个玩笑。

由按钮事件触发,采用window.open方法,打卡一个新页面

window.open('http://localhost/mtest/index.html', 'mtitle',
'height=100, width=400, top=0, left=0, toolbar=no, menubar=no, scrollbars=no,resizable=no,location=no, status=no');

在index.html采用如下方式监听浏览器被关闭了

为了更好的用户体验,同时在页面增加了一个按钮采用window.close方法,提醒用户闭关

document.getElementById("test").onclick=function(){
      window.close();
}
            
window.onbeforeunload=function(){
     event = event || window.event;
     event.returnValue="确定要关闭浏览器?";
     return "确定要关闭浏览器?";      
}

在onbeforeunload的中绑定需要触发的事件。


有关网页版的webchat在线聊天室大概都是这么处理的

最后,无论是刷新还是关闭,都要停止现在相关的服务。

原文地址:https://www.cnblogs.com/kuikui/p/2733014.html