Make Website Confirm Before Leaving

Long pause between blogs here but busy life = slow blog.

In this blog, I will discuss how I created a simple webpage that would allow me to leave a pinned tab open in my web browser that would create a popup box when trying to leave the site. The popup box can be seen below:

This is helpful because Chrome doesn’t give the option to make sure you actually want to close the window. So if you have ever, just like myself, been 2 hours deep into researching something that seemed really important at the time and then accidentally clicked the red X and have it all close, then you know how I feel.

So I created my own website that I would load and pin in the Chome windows so that this wouldn’t happen to me. This is the code that I used:

<script>
 window.onbeforeload = function(){
  return "Do you really want to leave?";
 };
</script>

and the site that I created is currently live at noautoclose.mharwood.uk

Just make sure to click anywhere on the page for the script to work, enjoy!


Leave a Reply