先引述一段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. ted飞行器

    http://v.youku.com/v_show/id_XNTc0MTk0MzI4.html

  2. HTTP Error 404.2 - Not Found The page you are requesting cannot be served because of the ISAPI and CGI Restriction list settings on the Web server(转)

    今天公司的同事問我,為什麼同一支程式在自己的電腦OK,部署到Server上會出現下面的錯誤 我想,沒有錯啊~ 我在這台Server所部署的程式一向都是OK的 看了錯誤的Error page, 發現是I ...

  3. 让linux history命令显示命令的运行时间、在哪个机器运行的这个命令

    1.在/etc/profile的最后加入例如以下部分: USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g ...

  4. ANDROID打包错误ERROR:EXECUTION FAILED FOR TASK ´:APP:LINTVITALRELEASE´.

    来自:http://dditblog.com/itshare_657.html 今天修改之前的项目之后.准备打包的时候.一起打包不了.一直提示有问题.错误是一些什么Strings.xml里面的一些信息 ...

  5. Source Insight 源代码查看工具

    在开发的过程中,有时候我们需要研究源代码,查看源码是一个好的习惯,能帮我们学到很多的东西,比如JDK可以帮助我们理解很多设计模式在实际开发中的应用,又或者android开发者,源代码更是必不可少的,当 ...

  6. struts2 action通配符

    首先,看一个struts2的配置文件: <package name="actions" extends="struts-default" namespac ...

  7. linux基础命令行

    1.ls ls //显示所有文件(不显示隐藏文件) ls -a //显示所有文件(显示隐藏文件) ls -l //显示所有文件详细信息 ls -S //以大小排序 ls -t //以时间排序 2.cd ...

  8. easyui datagrid 表格适应屏幕

    1.项目后台系统使用easyui,datagrid 的数据设置为自动适应屏幕,那么对于笔记本的话,就会显得有的小,可以通过设置datagrid属性,进行固定长度的设置 $('#gridTable'). ...

  9. c#如何操作ppt的播放 【转】

    C#控制ppt的代码 包括打开ppt文件.读取幻灯页,插入幻灯片,自动播放等 using System.Collections.Generic; using System.Linq; using Sy ...

  10. 恢复计算机崩溃数据的五款最佳Linux发行版

    嗨,Linux 新手们!你们在尝试运行命令时有没有搞坏过计算机系统?我相信你们有过这种经历.这一幕经常发生:你想尝试运行命令,或者安装测试更新版,结果下一次重启时计算机就崩溃了.我在本文将逐一介绍五款 ...