It is necessary that the block would pop up when trying to close the page. I tried many options from the stack, but not one did not work. Here are some examples:

window.onbeforeunload = confirmExit; function confirmExit() { var block = document.getElementById('leave'); block.classList.add('open') } $(window).bind('beforeunload', function() { alert('hello') }); 

This one earned:

 window.onbeforeunload = function(){ return "Don't leave me!"; }; 

But you need to customize it, but not to display the text. Are there any other options?

  • one
    this should not work by definition. Imagine that your function with the miraculous dialogue is buggy. And now, do not close the tab? On the other hand, the user can close the browser or turn off the computer. And this stick js'om impossible. - KoVadim

2 answers 2

This fragment when exactly works. When you try to close a tab, it will give a warning, but not all browsers allow you to change it, and display your message about closing the tab. For example, chrome says, Были изменения, вы уверены что хотите закрыть окно?

But you can not slip your own one, it is so pledged. Especially open your own custom modal window, not a personal browser window.

 window.onbeforeunload = function() { return "Что-нибудь сообщить пользователю"; } 

Previously, chrome also displayed a string that you can specify, but now it also prohibits. (At least in my latest version does not show my message)

  • Well, you need to shove a whole questionnaire about the reason for the refusal of services. Sorry, thanks - Nikita Shchypylov

As you have already answered: you can not change the window with the message when you close the tab. But you can do this for the desktop:

 $(document).ready(function() { var block = document.getElementById('leave'); $(block).on('click', function() { block.classList.remove('open'); }); $(document).mouseleave(function(e) { block.classList.add('open'); }); }); 
 #leave { width: 100px; height: 100px; position: absolute; top: 50px; left: 5px; border: 1px solid #000000; display: none; } #leave.open { display: block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="leave">Уже уходите?</div> 

I took the idea here: http://ruseller.com/lessons.php?rub=32&id=195