Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 875
zindarod Avatar asked Aug 14 '17 09:08

zindarod


People also ask

Why is my JavaScript not working on Chrome?

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.

Why JavaScript is not working in my browser?

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).


1 Answers

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.

like image 92
Karl Reid Avatar answered Sep 18 '22 00:09

Karl Reid