Close manual browser in JavaScript?

Is there a way to block the browser close event from JavaScript? I do not want to do this in the "Page Unload" event or something else. It needs to be processed when you click the close button of the browser.

Is it possible?

+4
source share
4 answers

No. You only have the onbeforeunload event available as source javascript (jquery does not include this event).

If you want to try, try sending an answer here and, without clicking "post your answer", try closing the browser window.

This is the closest way to access the close window event.

+12
source

The onbeforeunload event captures each unload event, but there is some trick to handle if the user closes the browser by clicking the close button, here is an example code

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script> $(document).ready(function(){ //handle if your mouse is over the document body, //if not your mouse is outside the document and if you click close button return message or do what you want var out = false; $("body").mouseover(function(){ out=false; }).mouseout(function(){ out=true; }); $(window).bind('beforeunload', function(e){ if(out) {return "Do you really want to leave this page now?"} }); }); </script> </head> <body> <p> <a href="http://cssbased.com">go outside</a> <br> <a href="test.html">reload page</a> <span> </span> </p> </body> </html> 
+3
source

Here is a good article about it from 4guysfromrolla

  <script language="JavaScript"> window.onbeforeunload = confirmExit; function confirmExit() { return message to display in dialog box; } </script> 
+1
source

Not really. The page unload event is all that you have. And even this is not always the case. This would provide an opportunity to interfere very badly with the wishes of users.

0
source

All Articles