<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>
<script>
// 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;
});
</script>
</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;
});
*/
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.