先引述一段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的更多相关文章

  1. 关于window.onload,window.onbeforeload与window.onunload

    ★  window.onload  当页面加载完毕的时候执行,即在当前页面进行其他操作之前执行.如,刚进入某个网页的弹窗提示. (  与window.onload相近的可以参考我写的另外一篇记录&qu ...

  2. window.onunload中使用HTTP请求

    在页面关闭时触发window.onunload 在onunload中要使用http请求,需要使用同步请求: 如: $.ajax({ url: url, async: false }); iframe页 ...

  3. .net mvc前台如何接收和解析后台的字典类型的数据 二分搜索算法 window.onunload中使用HTTP请求 网页关闭 OpenCvSharp尝试 简单爬虫

    .net mvc前台如何接收和解析后台的字典类型的数据   很久没有写博客了,最近做了一个公司门户网站的小项目,其中接触到了一些我不会的知识点,今日事情少,便记录一下,当时想在网上搜索相关的内容,但是 ...

  4. 关闭页面,window.onunload事件未执行的原因

    1.问题描述: JS中定义widow.onunload= function(),页面关闭时,logout()函数未执行. window.onunload = function() { logout() ...

  5. 浏览器关闭,onunload和onbeforeunload的使用

    系统描述:用户与管理员客服聊天,当用户在五分钟之内不说话,则客服会在5分钟后断开.当直接关闭浏览器页面时,后台管理员就无法检测到这种状态,此用户无法从管理员的服务列表里面清除 onunload和onb ...

  6. JS之onunload、onbeforeunload事件详解

    简介 onunload,onbeforeunload都是在刷新或关闭时调用,可以在<script>脚本中通过 window.onunload来调用.区别在于onbeforeunload在o ...

  7. 160503、onunload、onbeforeunload事件详解

    最近项目中做到一个功能:在上传页面用户开始上传文件之后用户点击任意跳转都需要弹出提示层进行二次确定才允许他进行跳转,这样做的目的是为了防止用户的错误操作导致这珍贵的UGC 流失(通常用户在一次上传不成 ...

  8. onunload、onbeforeunload事件详解--zhuan

    最近项目中做到一个功能:在上传页面用户开始上传文件之后用户点击任意跳转都需要弹出提示层进行二次确定才允许他进行跳转,这样做的目的是为了防止用户的错误操作导致这珍贵的UGC 流失(通常用户在一次上传不成 ...

  9. onunload与onbeforeunload事件解析 标签: 浏览器 2017-04-10 09:46 45人阅读 评论(0)

    注:只测试了chrome和IE浏览器的表现情况 onunload事件 //JS Document window.onunload = onunload_message; function onunlo ...

随机推荐

  1. Android Material Design-Working with Drawables(使用Drawable)-(五)

    转载请注明出处:http://blog.csdn.net/bbld_/article/details/40584331 翻译自:http://developer.android.com/trainin ...

  2. Restrict form resize -- Delphi

    http://www.delphipages.com/forum/showthread.php?t=58391 Hi, How would I restrict a form from being r ...

  3. 关于warning: Clock skew detected. Your build may be incomplete. 的解决方法

    今天发现电脑的系统时间不对,因此将时钟进行了改动,回头编译Linux kernel的时候,提演示样例如以下的warning: warning:  Clock skew detected.  Your ...

  4. 在Mac中设置Ctrl+C/V进行复制/粘贴

    从Windows世界走入Mac世界,最让不习惯的是在Mac中“复制/粘贴”的快捷键是Command+C/V.而且Command键与C/V键靠得太近,只能用大拇指与食指进行操作,也让人不习惯.再加上远程 ...

  5. 解决/dev/fb0无法打开的问题

    最近要在Linux做基于frame Buffer的图形显示,不论我在独立分区的Linux FC6系统中,还是在装有Red hat9的VPC中,都无法打开/dev/fb0.从网上找了很多资料,都没能解决 ...

  6. ThreadLocal实现session中用户信息 的线程间共享

    转载自:http://blog.sina.com.cn/s/blog_4b5bc01101013gok.html ThreadLocal并不难理解,我总结的最简单的理解就是: ThreadLocal像 ...

  7. 监听内容变化 TextWatcher @功能

    监听多个TextView的内容变化 使用示例 TextWatcherUtils.addTextChangedListener(isAllNotEmpty -> btnLogin.setEnabl ...

  8. Cognos TM1_10.1.1服务端安装

    出于对bi行业的强大热爱,出于对cognos tm1的强大兴趣,于是就想研究一下Cognos TM1(table manager one),今天就分享一下自己微不足道研究成果,真可谓是tm1的九牛一毛 ...

  9. dede 怎样调用其它栏目的文章或者缩略图列表且有分页效果?

    提问i:我做一个站点.有5个栏目,第5个栏目想把前4个栏目的文章都调用一下,搞一个汇总的文章集合. 发现用arclist调用不能设置pagesize的属性.从而不能给文章分页.然而list貌似不能调用 ...

  10. php的opcache缓存扩展(php页面代码刷新速度)

    opcache (全程 zend opcache): 从php5.5开始,默认提供的php脚本缓存扩展,编译php5.5时加上参数--enable-opcache就可以编译opcache了,只是要启用 ...