Search code examples
javascriptgoogle-chromeonunloadwindow.onunload

Javascript 'onunload' event not working in latest Version '54.0.2840.71 m' of Google Chrome browser


Recently I updated my Google Chrome browser to Version '54.0.2840.71 m' and suddenly javascript 'onUnload' event has stopped working. I am sure, it was working fine before the upgrade.

In my code (mentioned below), I am trying to open a 'Child' window from a 'Parent' window and when the child window is closed/refreshed (i.e. when its unload event is triggered), it should also reload or refresh the parent window.

Source code of parent.html file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Parent Page</title>
    </head>
    <body>
        This is parent page. Click <a href="#" onClick="window.open('child.html', '_blank', 'toolbar=no,scrollbars=yes,resizable=no,top=150,left=250,width=500,height=500'); return false;">here</a> to open the child page.
    </body>
</html>

Source Code of child.html file:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Child Page</title>
        <script type="text/javascript">
            window.onunload = refreshParent;
            function refreshParent() {
                window.opener.location.reload();
            }
        </script>
    </head> 
    <body>
        This is child page.
    </body>
</html>

This code works properly in both 'Internet Explorer' and 'Mozilla Firefox' browsers and it also used to work in earlier version of 'Google Chrome' but not its latest version.

Please let me know if I am doing something incorrectly.

Thanks & Regards!


Solution

  • I'm guessing that the event is actually called (as I can add breakpoints and do just about anything else in it) and that window.opener.location.reload() is just being ignored / prevented by this commit: https://chromium.googlesource.com/chromium/src/+/8fb70277dba468aac9d2eae51e432d76667a79db

    I've created a bug as well (https://bugs.chromium.org/p/chromium/issues/detail?id=660496) as I'm afraid your bug will be closed due to the previously mentioned discrepancy.