Javascript register window/tab closing event before window/tab close

前端 未结 4 1717
醉话见心
醉话见心 2020-12-09 05:33

This has / may have been asked before, but, as far as I remember, the question was not satisfactory answered at that time ^^

How can I register a window or tab closi

相关标签:
4条回答
  • 2020-12-09 06:00

    WE don't have Onclose but we have OnbeforeUnload. Even I am looking for same event but no luck.

    0 讨论(0)
  • 2020-12-09 06:02

    Unload and beforeUnload have negative effects on the "page cache" (also known as the "back forward cache"). This is a special cache that preserves some extra state when you navigate away from the page, so that it can be seamlessly resotred when you navigate 'back' (or unclose a tab). If you've ever wondered why some form fields keep their contents when you click back and some don't - this is often part of the reason.

    You should probably take a look at the "pageshow" and "pagehide" events in Firefox and WebKit. IE doesn't have a page cache equivalent, so in that environment you should be fine to use before/unload. Despite Opera's early concerns, pageshow and pagehide made it into the HTML5 Spec

    0 讨论(0)
  • 2020-12-09 06:04

    window.onbeforeunload= …;.

    0 讨论(0)
  • 2020-12-09 06:04
    window.onbeforeunload = function (e) {
      var e = e || window.event;
    
      // For IE and Firefox
      if (e) {
        e.returnValue = 'Any string';
      }
    
      // For Safari
      return 'Any string';
    };
    
    0 讨论(0)
提交回复
热议问题