prevent browser close with tabs open

by Laurens Maneschijn

HTML

<h1>
	Trick to prevent browser from closing with multiple windows open.
</h1>

<p>
	Make one pinned/sticky tab with a static html file with the script below in it.<br>

	Now if you close the browser you will probably get a notice that one of the pages is preventing your browser to close, and you can press ok/cancel to close browser or prevent all tabs from closing.<br>
	
	Note that you need to return a string (which is all you are allowed to do in this event handler function), but most browsers now tend to ignore said string and show a default message.
</p>
<pre>
&ltscript&gt;
//	window.onbeforeunload = function(e) {}
	window.addEventListener('beforeunload', function(e){
		e.returnValue = 'Would you really like to close this page (and your browser)?';
		return e.returnValue;
	});
&lt/script&gt;
</pre>

CSS

h1 { font-size: 1em; }

JavaScript

/*

//	window.onbeforeunload = function(e) {}
	window.addEventListener('beforeunload', function(e){
		e.returnValue = 'Would you really like to close this page (and your browser)?';
		return e.returnValue;
	});

*/