window.onbeforeunload doesn't work. #322

Closed
user4815162342 opened this Issue Jan 7, 2013 · 4 comments

Projects

None yet

4 participants

@user4815162342

Using node-webkit 0.3.7, the onbeforeunload function is not called to prevent a user from closing before saving changes. The following code is my test of this. In Firefox, this causes a Stay/Leave prompt to appear when I attempt to close the tab. In node-webkit, closing the window containing this code does not cause any message to pop up.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Testing</title>
    </head>
    <body>
        <p>This should warn before closing.</p>
        <script type="text/javascript">
            window.onbeforeunload = function() {
                return "Don't close me, please!";
            };
        </script>
    </body>
</html>
@zhchbin
Contributor
zhchbin commented Jan 8, 2013

@user4815162342 This will be triggered when you reload the page.However, closing the window directly does nothing as you said, which I think it could be a bug. BTW. here is a workaround to achieve the some function. You can read this wiki page for more detail.

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Testing</title>
    </head>
    <body>
        <p>This should warn before closing.</p>
        <script type="text/javascript">
          var gui = require('nw.gui');

          // Listen to main window's close event
          gui.Window.get().on('close', function() {
            var r = confirm("Do you really want to leave?");
            if (r == true) {
              this.close(true);
            } 
          });
        </script>
    </body>
</html>
@user4815162342

I thought something like that would work around it. Thanks.

@rogerwang rogerwang closed this Feb 12, 2014
@thejae
thejae commented Mar 26, 2014

Hello @rogerwang, we recently run into a similar problem, we are using @zhchbin workaround (thanks btw) but we are interested to know if there's anything in the pipeline to address the window.onbeforeunload issue.

The reason for this, is we are maintaining multiple set of codes for a 'cross platform' app. We'd like to minimize the use of platform specific workaround as much as possible. This helps transition between developers.

Thanks in advance!

@user4815162342

@thejae:

I added a compatibility layer to my project to handle this. Just keep all of your code specific to node-webkit in one file, or folder, with good comments, and new developers should be able to figure it out.

This is something like what I did (as always, test the code provided to make sure it works). It checks if the web page is running in node-webkit, if it is, then it will duplicate the functionality of onbeforeunload in the close event.

if (process && process.versions['node-webkit']) {
  var win = require('nw.gui').Window.get();
  win.on('close',function() {
     if (window.onbeforeunload) {
         var msg = window.onbeforeunload();
         if (msg) {
             if (!confirm(msg)) {
                return false;
             }
         }
         win.close(true);
     }
  });

}

Of course, this will only work if you are assigning to window.onbeforeunload, if you are using addEventListener (as you're supposed to), you'll have to do a bit more work to the code above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment