Prevent window closing using jQuery?

Is it possible to close the browser window using jQuery? I tried this:

$(document.ready(function() {
   $(window).unload(function(e) {
      e.preventDefault();
      alert("Not closing");
   });
});

The warning works, but the window closes anyway.

+3
source share
4 answers

Fortunately, this is not possible!

You can display a confirmation dialog with onbeforeunload(see, for example, here , how to do this), allowing the user not to leave the page at the end, but you cannot prevent closing against the user.

+5
source

This would be a huge security issue, and although I never had to personally investigate it, I doubt that any browser will allow you to close the window.

+2
source

This will be a fundamental violation of user autonomy.! How do you like it if the site forbids you to close your browser?

It is best to show a confirmation dialog, as Pekka suggests; you can ask them if they are sure, but ultimately their decision if they want to close it!

0
source

as other answers say that it is impossible to prevent, but you can give the user the opportunity to choose. Maybe you are looking for this ...

How can I override the OnBeforeUnload dialog and replace it with my own?

0
source

Source: https://habr.com/ru/post/1776486/


All Articles