zoukankan      html  css  js  c++  java
  • 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.

    解决兼容性:

       

    1 var myEvent = window.attachEvent || window.addEventListener;
    2     var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
    3 
    4             myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
    5                 var confirmationMessage = 'Are you sure to leave the page?';  // a space
    6                 (e || window.event).returnValue = confirmationMessage;
    7                 return confirmationMessage;
    8  });
  • 相关阅读:
    轻量级调试api接口 Jquery.PlayingAPI v 1.0
    js验证整数加保留小数点
    简单漂亮bootstrap grid列表分页 demo.
    polemo-logger 分析
    POSIX 标准的获取(pdf 格式)
    游戏开发利器(工具、资源)
    仅有 265 行的第一人称引擎
    介绍一款非常适合做微网站并且免费的CMS系统
    兔子无限繁殖问题——婓波那契数列
    近似计算
  • 原文地址:https://www.cnblogs.com/yiliweichinasoft/p/4061737.html
Copyright © 2011-2022 走看看