beforeunload
event is fired when the window, the document and its resources are about to be unloaded.
The document is still visible and the event is still cancelable at this point.
| 冒泡 | No |
|---|---|
| 可取消 | Yes |
| 接口 |
事件
|
| 事件处理程序特性 |
onbeforeunload
|
This event enables a web page to trigger a confirmation dialog asking the user if they really want to leave the page. If the user confirms, the browser navigates to the new page, otherwise it cancels the navigation.
According to the specification, to show the confirmation dialog an event handler should call
preventDefault()
on the event.
However note that not all browsers support this method, and some instead require the event handler to implement one of two legacy methods:
returnValue
property
Some browsers used to display the returned string in the confirmation dialog, enabling the event handler to display a custom message to the user. However, this is deprecated and no longer supported in most browsers.
To combat unwanted pop-ups, browsers may not display prompts created in
beforeunload
event handlers unless the page has been interacted with, or may even not display them at all.
Attaching an event handler/listener to
window
or
document
's
beforeunload
event prevents browsers from using in-memory page navigation caches, like
Firefox's Back-Forward cache
or
WebKit's Page Cache
.
The HTML specification states that calls to
window.alert()
,
window.confirm()
,和
window.prompt()
methods may be ignored during this event. See the
HTML specification
了解更多细节。
The HTML specification states that authors should use the
Event.preventDefault()
method instead of using
Event.returnValue
. However, this is not supported by all browsers.
window.addEventListener('beforeunload', (event) => {
// Cancel the event as stated by the standard.
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = '';
});
| 规范 | 状态 | 注释 |
|---|---|---|
|
HTML 实时标准
The definition of 'beforeunload' in that specification. |
实时标准 | |
|
HTML5
The definition of 'beforeunload' in that specification. |
推荐 | 初始定义 |
| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
beforeunload
event
|
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 |
Activation using
event.returnValue = "string";
弃用
|
Chrome 30 | Edge 12 | Firefox Yes | IE Yes | Opera ? | Safari ? | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android Yes | Safari iOS No | Samsung Internet Android Yes |
Activation using
event.preventDefault()
|
Chrome No | Edge 12 — 79 | Firefox Yes | IE 9 | Opera No | Safari 11 | WebView Android No | Chrome Android No | Firefox Android Yes | Opera Android No | Safari iOS No | Samsung Internet Android No |
Activation using
return "string";
弃用
|
Chrome 1 | Edge 12 | Firefox 1 | IE Yes | Opera 12 | Safari 3 | WebView Android Yes | Chrome Android Yes | Firefox Android Yes | Opera Android Yes | Safari iOS No | Samsung Internet Android Yes |
完整支持
不支持
兼容性未知
非标。预期跨浏览器支持较差。
弃用。不要用于新网站。
见 WindowEventHandlers/onbeforeunload for more details on how various browsers handle this event.
DOMContentLoaded
,
readystatechange
,
load
,
unload
Window
applicationCache
caches
closed
console
controllers
crossOriginIsolated
crypto
customElements
defaultStatus
devicePixelRatio
dialogArguments
目录
document
event
frameElement
frames
fullScreen
history
indexedDB
innerHeight
innerWidth
isSecureContext
isSecureContext
length
localStorage
location
locationbar
menubar
mozAnimationStartTime
mozInnerScreenX
mozInnerScreenY
mozPaintCount
名称
navigator
onabort
onafterprint
onanimationcancel
onanimationend
onanimationiteration
onappinstalled
onauxclick
onbeforeinstallprompt
onbeforeprint
onbeforeunload
onblur
oncancel
oncanplay
oncanplaythrough
onchange
onclick
onclose
oncontextmenu
oncuechange
ondblclick
ondevicelight
ondevicemotion
ondeviceorientation
ondeviceorientationabsolute
ondeviceproximity
ondragdrop
ondurationchange
onended
onerror
onfocus
onformdata
ongamepadconnected
ongamepaddisconnected
ongotpointercapture
onhashchange
oninput
oninvalid
onkeydown
onkeypress
onkeyup
onlanguagechange
onload
onloadeddata
onloadedmetadata
onloadend
onloadstart
onlostpointercapture
onmessage
onmessageerror
onmousedown
onmouseenter
onmouseleave
onmousemove
onmouseout
onmouseover
onmouseup
onmozbeforepaint
onpaint
onpause
onplay
onplaying
onpointercancel
onpointerdown
onpointerenter
onpointerleave
onpointermove
onpointerout
onpointerover
onpointerup
onpopstate
onrejectionhandled
onreset
onresize
onscroll
onselect
onselectionchange
onselectstart
onstorage
onsubmit
ontouchcancel
ontouchstart
ontransitioncancel
ontransitionend
onunhandledrejection
onunload
onuserproximity
onvrdisplayactivate
onvrdisplayblur
onvrdisplayconnect
onvrdisplaydeactivate
onvrdisplaydisconnect
onvrdisplayfocus
onvrdisplaypointerrestricted
onvrdisplaypointerunrestricted
onvrdisplaypresentchange
onwheel
opener
origin
outerHeight
outerWidth
pageXOffset
pageYOffset
parent
性能
personalbar
pkcs11
screen
screenLeft
screenTop
screenX
screenY
scrollbars
scrollMaxX
scrollMaxY
scrollX
scrollY
self
sessionStorage
sidebar
speechSynthesis
status
statusbar
toolbar
top
visualViewport
window
alert()
atob()
back()
blur()
btoa()
cancelAnimationFrame()
cancelIdleCallback()
captureEvents()
clearImmediate()
clearInterval()
clearTimeout()
close()
confirm()
convertPointFromNodeToPage()
convertPointFromPageToNode
createImageBitmap()
dump()
fetch()
find()
focus()
forward()
getAttention()
getComputedStyle()
getDefaultComputedStyle()
getSelection()
home()
matchMedia()
minimize()
moveBy()
moveTo()
open()
openDialog()
postMessage()
print()
prompt()
queueMicrotask()
releaseEvents()
requestAnimationFrame()
requestFileSystem()
requestIdleCallback()
resizeBy()
resizeTo()
restore()
routeEvent()
scroll()
scrollBy()
scrollByLines()
scrollByPages()
scrollTo()
setCursor()
setImmediate()
setInterval()
setTimeout()
showModalDialog()
sizeToContent()
stop()
updateCommands()
event
afterprint
animationcancel
animationend
animationiteration
beforeprint
beforeunload
blur
copy
cut
DOMContentLoaded
error
focus
hashchange
languagechange
load
message
messageerror
offline
online
orientationchange
pagehide
pageshow
paste
popstate
rejectionhandled
storage
transitioncancel
unhandledrejection
unload
vrdisplayconnect
vrdisplaydisconnect
vrdisplaypresentchange