JavaScript not working after Chrome update


I often accidentally close my Chrome browser and have to reopen and reload all the tabs that I'd been working on. Since Chrome does not a have built in confirm before closing mechanism, I wrote a simple page to ask for confirmation before closing. I leave that page open among my other tabs.

<!DOCTYPE html>
<html>
    <body>
    <p>This page is to prevent accidental closing of Chrome.</p>
        <script language="JavaScript">

            window.onbeforeunload = function () {
                return "Are you sure?";
            };
        </script>
    </body>
</html>

Recently I updated my Chrome browser from version 56 to 60. Now the code doesn't seem to work as it no longer asks for confirmation before closing. I tried many different variations from the internet but none seem to work.

Note: I am very new to web development.

According to the MDN docs :

To combat unwanted pop-ups, some browsers don't display prompts created in beforeunload event handlers unless the page has been interacted with; some don't display them at all.

So, I think your function is not guaranteed to run, especially because Chrome 60 explicitly has that first behaviour. From the notes:

From Chrome 60 onward, the beforeunload dialog will only appear if the frame attempting to display it has received a user gesture or user interaction (or if any embedded frame has received such a gesture).

So if you want to continue using this approach, you may need to interact with the page at some point in your session.

Alternatively, to open all the tabs you had open when you restart Chrome, try pressing Ctrl-Shift-T.