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.
Google ChromeIn the "Settings" section click on the "Show advanced settings..." Under the the "Privacy" click on the "Content settings...". When the dialog window opens, look for the "JavaScript" section and select "Allow all sites to run JavaScript (recommended)". Click on the "OK" button to close it.
Go to the web browser menu click on the Customize and control Google Chrome. Next, select Settings and click on the Show advanced settings. Under Privacy click on the Content settings. Locate the JavaScript section and select Allow all sites to run JavaScript (recommended).
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With