beforeunload
이벤트는 윈도우, 문서 및 리소스가 언로드 될 때 발생합니다. 문서의 내용은 여전히 표시되며 이 시점에서 이벤트는 아직 취소 가능합니다.
만약 returnValue
이벤트 프로퍼티에 문자열을 할당했다면, 유저에게 페이지를 나갈지 확인하는 대화상자에 나타납니다. 어떤 브라우저는 대화상자에 문자열이 보이지만, 다른 브라우저에서는 그들의 자체 메시지가 보입니다. 아무것도 할당하지 않는다면 이 이벤트는 대화상자 없이 조용히 진행됩니다.
Note: 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.
Bubbles | No |
Cancelable | Yes |
Target objects | defaultView |
Interface | Event |
Properties
Property | Type | Description |
---|---|---|
target Read only |
EventTarget |
The event target (the topmost target in the DOM tree). |
type Read only |
DOMString |
The type of event. |
bubbles Read only |
Boolean |
Does the event normally bubble? |
cancelable Read only |
Boolean |
Is it possible to cancel the event? |
returnValue |
DOMString |
The current return value of the event (the message to show the user). |
Examples
window.addEventListener("beforeunload", function (event) { event.returnValue = "\\o/"; }); // is equivalent to window.addEventListener("beforeunload", function (event) { event.preventDefault(); });
Notes
Since 25 May 2011, the HTML5 specification states that calls to window.alert()
, window.confirm()
, and window.prompt()
methods may be ignored during this event. See the HTML5 specification for more details.
Various browsers ignore the result of the event and do not ask the user for confirmation at all. The document will always be unloaded automatically. Firefox has a switch named dom.disable_beforeunload
in about:config to enable this behaviour.
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'beforeunload' in that specification. |
Living Standard | |
HTML5 The definition of 'beforeunload' in that specification. |
Recommendation | Initial definition |
Browser compatibility
Feature | Chrome | Edge | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
---|---|---|---|---|---|---|
Basic support | 1.0 | (Yes) | 1 | 4 | 12 | 3 |
Custom text support removed | 51.0 | No support | 44.0 (44.0) | 38 | 9.1 |
Feature | Android | Android Webview | Edge | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | Chrome for Android |
---|---|---|---|---|---|---|---|---|
Basic support | ? | (Yes) | (Yes) | ? | ? | ? | (no) defect | (Yes) |
Custom text support removed | ? | 51.0 | No support | 44.0 (44.0) | 51.0 |