window.onunload | window.onbeforeunload
先引述一段jQuery 官方对于onunload的评述:
The unload event is sent to the window element when the user navigates away from the page. This could mean one of many things. The user could have clicked on a link to leave the page, or typed in a new URL in the address bar. The forward and back buttons will trigger the event. Closing the browser window will cause the event to be triggered. Even a page reload will first create an unload event.
The exact handling of the unload event has varied from version to version of browsers. For example, some versions of Firefox trigger the event when a link is followed, but not when the window is closed. In practical usage, behavior should be tested on all supported browsers, and contrasted with the proprietary beforeunload event.
Any unload event handler should be bound to the window object:
|
1
2
3
|
$( window ).unload(function() { alert( "Handler for .unload() called." );}); |
After this code executes, the alert will be displayed whenever the browser leaves the current page. It is not possible to cancel the unload event with .preventDefault(). This event is available so that scripts can perform cleanup when the user leaves the page.
解决兼容性:
var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
var confirmationMessage = 'Are you sure to leave the page?'; // a space
(e || window.event).returnValue = confirmationMessage;
return confirmationMessage;
});
window.onunload | window.onbeforeunload的更多相关文章
- 关于window.onload,window.onbeforeload与window.onunload
★ window.onload 当页面加载完毕的时候执行,即在当前页面进行其他操作之前执行.如,刚进入某个网页的弹窗提示. ( 与window.onload相近的可以参考我写的另外一篇记录&qu ...
- window.onunload中使用HTTP请求
在页面关闭时触发window.onunload 在onunload中要使用http请求,需要使用同步请求: 如: $.ajax({ url: url, async: false }); iframe页 ...
- .net mvc前台如何接收和解析后台的字典类型的数据 二分搜索算法 window.onunload中使用HTTP请求 网页关闭 OpenCvSharp尝试 简单爬虫
.net mvc前台如何接收和解析后台的字典类型的数据 很久没有写博客了,最近做了一个公司门户网站的小项目,其中接触到了一些我不会的知识点,今日事情少,便记录一下,当时想在网上搜索相关的内容,但是 ...
- 关闭页面,window.onunload事件未执行的原因
1.问题描述: JS中定义widow.onunload= function(),页面关闭时,logout()函数未执行. window.onunload = function() { logout() ...
- 浏览器关闭,onunload和onbeforeunload的使用
系统描述:用户与管理员客服聊天,当用户在五分钟之内不说话,则客服会在5分钟后断开.当直接关闭浏览器页面时,后台管理员就无法检测到这种状态,此用户无法从管理员的服务列表里面清除 onunload和onb ...
- JS之onunload、onbeforeunload事件详解
简介 onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来调用.区别在于onbeforeunload在o ...
- 160503、onunload、onbeforeunload事件详解
最近项目中做到一个功能:在上传页面用户开始上传文件之后用户点击任意跳转都需要弹出提示层进行二次确定才允许他进行跳转,这样做的目的是为了防止用户的错误操作导致这珍贵的UGC 流失(通常用户在一次上传不成 ...
- onunload、onbeforeunload事件详解--zhuan
最近项目中做到一个功能:在上传页面用户开始上传文件之后用户点击任意跳转都需要弹出提示层进行二次确定才允许他进行跳转,这样做的目的是为了防止用户的错误操作导致这珍贵的UGC 流失(通常用户在一次上传不成 ...
- onunload与onbeforeunload事件解析 标签: 浏览器 2017-04-10 09:46 45人阅读 评论(0)
注:只测试了chrome和IE浏览器的表现情况 onunload事件 //JS Document window.onunload = onunload_message; function onunlo ...
随机推荐
- unix-软件安装
检查软件是否已经安装 rpm -qa|grep 软件标识名 在线安装软件 yum -y install 软件标识名
- gridview 空数据绑定
private void getDataBind() { DataTable dt = new DataTable(); dt.Columns.Add("id"); dt.Colu ...
- Lower dc/dc-converter ripple by using optimum capacitor hookup
Low-ripple-voltage positive-to-negative dc/dc converters find use in many of today's high- frequency ...
- 用css让一个容器水平垂直居中
阅读目录 方法一:position加margin 方法二: diaplay:table-cell 方法三:position加 transform 方法四:flex;align-items: cente ...
- linkhashmap实现原理
HashMap和双向链表合二为一即是LinkedHashMap.所谓LinkedHashMap,其落脚点在HashMap,因此更准确地说,它是一个将所有Entry节点链入一个双向链表的HashMap. ...
- Go:如何组织代码
备注 此文主要是对官方教程的学习总结,官方教程:http://golang.org/doc/code.html. 工作空间(Workspaces) Go代码必须保持在一个workspace,works ...
- inno setup检查是否已经安装
[Registry] Root: HKLM; Subkey: "Software\MCS"; ValueType: string; ValueName: "MCSVers ...
- Unity3d通用工具类之数据配置加载类
今天,我们来讲讲游戏中的数据配置加载. 什么是游戏数据加载呢?一般来说游戏中会有场景地图. 按照国际惯例,先贴一张游戏场景的地图: 在这张地图上,我们可以看到有很多正六边形,正六边形上有树木.岩石等. ...
- CentOS6.5配置网络
[ CleverCode发表在csdn博客中的原创作品,请勿转载.原创地址:http://blog.csdn.net/clevercode/article/details/46376985] 1 网卡 ...
- github pages+hexo自建博客
1.github创建新项目,然后开启pages即可 2.全局安装hexo npm install -g hexo 3.初始化hexo hexo init 4.安装hexo的依赖 npm i 5.基本上 ...