欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

掌握浏览器常用功能:右键点击、刷新页面、默认事件处理和新窗口开启与关闭

最编程 2024-02-03 11:07:50
...

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在线聊天室大概都是这么处理的

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