onbeforeunload
特性为
WindowEventHandlers
混合
EventHandler
为处理
beforeunload
events. These events fire when a window is about to
unload
its resources. At this point, the document is still visible and the event is still cancelable.
注意:
To combat unwanted pop-ups, some browsers don't display prompts created in
beforeunload
event handlers unless the page has been interacted with. Moreover, some don't display them at all.
window.addEventListener("beforeunload", function(event) { ... });
window.onbeforeunload = function(event) { ... };
Typically, it is better to use
window.addEventListener()
和
beforeunload
event, instead of
onbeforeunload
.
This example prompts the user before unloading.
The HTML specification states that authors should use the
Event.preventDefault()
method instead of using
Event.returnValue
to prompt the user.
window.addEventListener('beforeunload', function (e) {
// Cancel the event
e.preventDefault(); // If you prevent default behavior in Mozilla Firefox prompt will always be shown
// Chrome requires returnValue to be set
e.returnValue = '';
});
Guarantee the browser unload by removing the returnValue property of the event
window.addEventListener('beforeunload', function (e) {
// the absence of a returnValue property on the event will guarantee the browser unload happens
delete e['returnValue'];
});
When your page uses JavaScript to render content, the JavaScript may stop when leaving and then navigating back to the page. You can bind to
window.onbeforeunload
to prevent the browser from fully caching the page. If you do so, JavaScript in the page will be triggered on the subsequent return visit and update the content as desired.
The event was originally introduced by Microsoft in Internet Explorer 4 and standardized in the HTML5 specification.
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'onbeforeunload' in that specification. |
实时标准 | |
|
HTML 5.1
在该规范中的 GlobalEventHandlers 定义。 |
推荐 | |
|
HTML5
在该规范中的 GlobalEventHandlers 定义。 |
推荐 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
onbeforeunload
|
Chrome 1 | Edge 12 | Firefox 1 | IE 4 | Opera 12 | Safari 3 | WebView Android 1 | Chrome Android 18 | Firefox Android 4 | Opera Android 12 | Safari iOS 1 | Samsung Internet Android 1.0 |
| Custom text support 弃用 非标 | Chrome ? — 51 | Edge No | Firefox ? — 44 | IE Yes | Opera ? — 38 | Safari ? — 9 | WebView Android ? — 51 | Chrome Android ? — 51 | Firefox Android ? — 44 | Opera Android ? — 41 | Safari iOS No | Samsung Internet Android ? — 5.0 |
完整支持
不支持
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
The HTML specification states that authors should use the
Event.preventDefault()
method instead of using
Event.returnValue
to prompt the user. However, this is not yet supported by all browsers.
When this event returns (or sets the
returnValue
property to) a value other than
null
or
undefined
, the user will be prompted to confirm the page unload. In older browsers, the return value of the event is displayed in this dialog. Starting with Firefox 44, Chrome 51, Opera 38, and Safari 9.1, a generic string not under the control of the webpage will be shown instead of the returned string. For example:
Internet Explorer does not respect the
null
return value and will display this to users as "null" text. You have to use
undefined
to skip the prompt.
In some browsers, calls to
window.alert()
,
window.confirm()
,和
window.prompt()
may be ignored during this event. See the
HTML specification
了解更多细节。
Note also, that various browsers ignore the result of the event and do not ask the user for confirmation at all. In such cases, the document will always be unloaded automatically. Firefox has a switch named
dom.disable_beforeunload
in
about:config
to enable this behaviour. As of Chrome 60, the confirmation
will be skipped
if the user has not performed a gesture in the frame or page since it was loaded. Pressing F5 in the page seems to count as user interaction, whereas mouse-clicking the refresh arrow or pressing F5 with Chrome DevTools focused does not count as user interaction (as of Chrome 81).
WindowEventHandlers
BeforeUnloadEvent
DOMStringMap
ErrorEvent
GlobalEventHandlers
HTMLAnchorElement
HTMLAreaElement
HTMLAudioElement
HTMLBRElement
HTMLBaseElement
HTMLBaseFontElement
HTMLBodyElement
HTMLButtonElement
HTMLCanvasElement
HTMLContentElement
HTMLDListElement
HTMLDataElement
HTMLDataListElement
HTMLDialogElement
HTMLDivElement
HTMLDocument
HTMLElement
HTMLEmbedElement
HTMLFieldSetElement
HTMLFormControlsCollection
HTMLFormElement
HTMLFrameSetElement
HTMLHRElement
HTMLHeadElement
HTMLHeadingElement
HTMLHtmlElement
HTMLIFrameElement
HTMLImageElement
HTMLInputElement
HTMLIsIndexElement
HTMLKeygenElement
HTMLLIElement
HTMLLabelElement
HTMLLegendElement
HTMLLinkElement
HTMLMapElement
HTMLMediaElement
HTMLMetaElement
HTMLMeterElement
HTMLModElement
HTMLOListElement
HTMLObjectElement
HTMLOptGroupElement
HTMLOptionElement
HTMLOptionsCollection
HTMLOutputElement
HTMLParagraphElement
HTMLParamElement
HTMLPictureElement
HTMLPreElement
HTMLProgressElement
HTMLQuoteElement
HTMLScriptElement
HTMLSelectElement
HTMLShadowElement
HTMLSourceElement
HTMLSpanElement
HTMLStyleElement
HTMLTableCaptionElement
HTMLTableCellElement
HTMLTableColElement
HTMLTableDataCellElement
HTMLTableElement
HTMLTableHeaderCellElement
HTMLTableRowElement
HTMLTableSectionElement
HTMLTemplateElement
HTMLTextAreaElement
HTMLTimeElement
HTMLTitleElement
HTMLTrackElement
HTMLUListElement
HTMLUnknownElement
HTMLVideoElement
HashChangeEvent
历史
ImageData
定位
MessageChannel
MessageEvent
MessagePort
Navigator
NavigatorGeolocation
NavigatorID
NavigatorLanguage
NavigatorOnLine
NavigatorPlugins
PageTransitionEvent
Plugin
PluginArray
PopStateEvent
PortCollection
PromiseRejectionEvent
RadioNodeList
Transferable
ValidityState
Window
WindowBase64
WindowTimers