В процессе перевода.


Свойство WindowEventHandlers.onbeforeunload сожержит код, который вполняется при отправке события beforeunload. Событие вызывается перед закрытием страницы, когда страница осовобождает свои ресурсы. При этом документ остаётся видимым и событие возможно отменить.

Note: To combat unwanted pop-ups, browsers may not display prompts created in beforeunload event handlers unless the page has been interacted with. For a list of specific browsers, see the Browser compatibility section.


window.onbeforeunload = funcRef
  • funcRef is a reference to a function or a function expression.
  • The function should assign a string value to the returnValue property of the Event object and return the same string.


window.onbeforeunload = function(e) {
  var dialogText = 'Dialog text here';
  e.returnValue = dialogText;
  return dialogText;


When this event returns (or sets the returnValue property to) a value other than null or undefined, the user is prompted to confirm the page unload. In some browsers, the return value of the event is displayed in this dialog. Starting with Firefox 4, 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, Firefox displays the string "This page is asking you to confirm that you want to leave - data you have entered may not be saved." See баг 588292 and Chrome Platform Status.

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.

Note also, that various mobile browsers ignore the result of the event (that is, they do not ask the user for confirmation). Firefox has a hidden preference in about:config to do the same. In essence this means the user always confirms that the document may be unloaded.

You can and should handle this event through window.addEventListener() and the beforeunload event. More documentation is available there.


The event was originally introduced by Microsoft in Internet Explorer 4 and standardized in the HTML5 specification.

Specification Status Comment
WHATWG HTML Living Standard
Определение 'GlobalEventHandlers' в этой спецификации.
Живой стандарт  
HTML 5.1
Определение 'GlobalEventHandlers' в этой спецификации.
Определение 'GlobalEventHandlers' в этой спецификации.

Browser compatibility

Feature Chrome Edge Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1.0 (Да) 1 4 12 3
Custom text support removed 51.0 Нет 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 ? (Да) (Да) ? ? ? (no) defect (Да)
Custom text support removed ? 51.0 Нет 44.0 (44.0)       51.0

See also

Метки документа и участники

 Внесли вклад в эту страницу: vitalyzhakov, artemderzkiy, rtf-const
 Обновлялась последний раз: vitalyzhakov,