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

前端 未结 4 1719
醉话见心
醉话见心 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: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

提交回复
热议问题