先引述一段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. require(): open_basedir restriction in effect. 解决方法

    在linux服务器部署thinkphp5的时候PHP报了这个错误, 如下: Warning: require(): open_basedir restriction in effect. File(/ ...

  2. LT3759 宽输入电压范围升压 / 负输出 / SEPIC 控制器 可在输入电压低至 1.6V 的情况下工作

    引言当今的许多电子设备都需要一个负输出或正输出转换器,有时则是两者均需要.另外,它们还必需采用各种电源运作,包括 USB.墙上适配器.碱性电池和锂电池等. 为了从可变输入电压产生不同极性的输出,电源设 ...

  3. 配置druid内置的log实现

    Druid不依赖任何的log组件,但支持多种log组件,会根据检测当前环境,选择一种合适的log实现. log的优先顺序 log4j -> log4j2 -> slf4j -> co ...

  4. 要使用C#实现一个ActiveX控件

    要使用C#实现一个ActiveX控件,需要解决三个问题: 1.使.NET组件能够被COM调用 2.在客户机上注册后,ActiveX控件能通过IE的安全认证 3.未在客户机上注册时,安装包能通过IE的签 ...

  5. Can't deserialize with binaryFormatter after changing namespace of class

    After changing the namespace of my class I can no longer deserialize the objects. I've implemented S ...

  6. openfire Hazelcast插件集群配置

    原文:http://blog.csdn.net/frankcheng5143/article/details/48708899 注意虽然hazelcast 官方已经有了3.5.2版本,但是openfi ...

  7. linux集成开发环境

    Linux操作系统的种种集成开发环境 随着Linux的逐渐兴起,已经有为数众多的程序在上面驰骋了,许多开发环境(Development Environment)也应运而生.好的开发环境一定是集成了编辑 ...

  8. Xcode 安装插件管理器

    https://github.com/alcatraz/Alcatraz运行之后, load bundle, 然后window就有pakagegemanage. 下载如下插件: 自动导入插件

  9. javascript比较两个时间大小

    //var yourtime=document.getElementById('begin_time').value; var yourtime='2010-12-10 11:12'; yourtim ...

  10. 《iOS应用逆向工程:分析与实战》

    <iOS应用逆向工程:分析与实战> 基本信息 作者: 沙梓社    吴航    刘瑾 丛书名: 信息安全技术丛书 出版社:机械工业出版社 ISBN:9787111450726 上架时间:2 ...