Javascript Alert Pop up Box For Accidental Navigation / Close

Here’s an easy javascript alert pop up box / warning box if you accidentally close or leave the current tab

You have your moments that you accidentally close a tab and boom all you work has gone into thin air. As a programmer this is a pain in the ass. But worry no more, today I’ll show to you the most effective yet very easy javascript code that you can easily copy and paste on your own own source code. A code that will save you! 


A simple javascript code that will give you warning if you accidentally close/leave a tab you currently in.

That you can use on your marketing needs and don’t let your customers leave you site!

For programming needs that you dont want users to accidentally close the tab.


window.onbeforeunload = function (e) {
    e = e || window.event;

    // For IE and Firefox prior to version 4
    if (e) {
        e.returnValue =';Are you sure you want to leave?';

    // For Safari
    return 'Are you sure you want to leave?';

How to use?

Simply copy the code above and paste anywhere on your source code that you want to show the warning pop up box. Preferrable inside the body tag.

Leave a Comment