Skip to content

Make beforeunload event (largely) web-compatible while maintain backward compatibility #2579

Closed

Description

@duanyao

Current behavior of beforeunload event in Electron may break existing web pages. However, changing the behavior according to the spec may break existing Electron apps.

This is a tradeoff proposed by @deepak1556 in #2481 to make both sides happy:

// do what Electron does currently: unload the page immediately.
window.addEventListener('beforeunload', function(ev) { ev.returnValue = 'true' });
// do what Electron does currently: prevent the unload.
window.addEventListener('beforeunload', function(ev) { ev.returnValue = 'false' });

 // do what the spec says: ask the user for confirmation if the string is not empty
window.addEventListener('beforeunload', function(ev) { ev.returnValue =
    a_string_other_than_true_false });

Non-string values, specifically true/false, are converted to their string forms.

If window.onbeforeunload is used instead, the return value of the handler has same effect of ev.returnValue.

A remaining question is what to do with ev.preventDefault(). The spec says the UA should ask the user for confirmation. However my test showed that Electron 0.30.4 on Windows allows reload even if ev.preventDefault() is called. So I think it is OK to implement ev.preventDefault() according to the spec?

Activity

  1. etiktin commented on Aug 25, 2015

    @etiktin
    Contributor

    When implementing the default behavior for a non empty string it's important to make sure that the confirm navigation dialog will appear no more than once for a single event propagation (if the parent window returned a string and it was displayed in a dialog, you don't want the child window to display another one - see details at #2361).

    I'm not sure about ev.preventDefault(). My main problem is that in Electron you can actually cancel the unload by returning false. To me, that fits better with the semantic meaning of preventing the default which according to MDN it's to:

    Cancel the event if it is cancelable, without stopping further propagation of the event

    That said it might break code transferred from an existing website, but I doubt many actually use this (most probably prefer to set the string that will be displayed in the dialog).

  2. duanyao commented on Aug 26, 2015

    @duanyao
    Author

    @etiktin Firefox doesn't show the string of BeforeUnloadEvent.returnValue at all, because they believe it is a security issue (see https://bugzilla.mozilla.org/show_bug.cgi?id=588292#c2). So I suspect there are some real uses of BeforeUnloadEvent.preventDefault().

  3. duanyao commented on Aug 26, 2015

    @duanyao
    Author

    @etiktin To avoid multiple confirmation when iframes involved, I think Electron may suppress any dialogs that iframes try to create during beforeunload events, if the parent has at least one beforeunload handler. Not sure throwing or pretending canceled is better.

  4. etiktin commented on Aug 26, 2015

    @etiktin
    Contributor

    To avoid multiple confirmation when iframes involved, I think Electron may suppress any dialogs that iframes try to create during beforeunload events, if the parent has at least one beforeunload handler.

    @duanyao, that's exactly what I meant. This is how Chrome and CEF do it. CEF even prints the following error when you try to open another confirm navigation dialog in a child window:
    Blocked attempt to show multiple 'beforeunload' confirmation panels for a single navigation

  5. twolfson commented on Jan 22, 2017

    @twolfson
    Contributor

    I have been redirected here from #8468 but it's unclear what the conclusion in this thread was. Can someone reiterate what the preferred behavior is?

    For example:

    • Always show top level dialogue and ignore iframe dialogues
    • Show dialogues for all levels
  6. 22222 commented on Apr 28, 2017

    @22222
    Contributor

    It looks like things have changed since this ticket was created. The message returned by the beforeunload event handlers is no longer available where the Electron code was checking it as of the Chrome 51 API Changes from Chromium issue 587940. That might change what a solution for this issue needs to do.

    Unless I missed something, Electron should already be acting just like Chromium for the actual event handling part (possibly through the code in the WebKit Document class). What's missing is a way of providing a confirmation dialog in atom_javascript_dialog_manager.cc.

    The Chromium browser would display a dialog to the user here to let them decide whether to actually prevent the unload or not. At the time this issue was created, Electron checked if the message was an empty string or "false", but otherwise always prevented the unload (with no user confirmation or other dialog displayed). Now that the message text is no longer available, Electron is hardcoded to always prevent the unload when things get this far.

    Personally, I wouldn't want Electron to display a confirmation dialog like Chromium does by default. The main problem is that the messages in the dialog would have to come from built-in strings, and that means Electron would probably have to have translations for all of those strings (like how Chromium reads those strings from a resource text file). And while it makes sense to not have control over the messages in a browser, I'd probably want to be able to decide exactly what I'm displaying to the user in an Electron app.

    So I'd propose a new event or some kind of other hook to let an app provide their own confirmation dialog (or do whatever kind of checks they want). My idea would be a will-prevent-unload event for BrowserWindow.webContents, where you can call event.preventDefault() to let the user leave the page even if a beforeunload event tried to cancel the page unload.

    With that event, implementing a confirmation like in Chromium would look like this:

    const {BrowserWindow} = require('electron')
    let win = new BrowserWindow()
    win.webContents.on('will-prevent-unload', (event) => {
      let choice = dialog.showMessageBox(mainWindow, {
        type: 'question',
        buttons: ['Leave', 'Stay'],
        title: 'Do you want to leave this site?',
        message: 'Changes you made may not be saved.',
        defaultId: 0,
        cancelId: 1
      })
      let leave = (choice === 0)
      if (leave) {
        event.preventDefault()
      }
    })

    And another advantage of that kind of change is that it would be backwards compatible. If you don't add one of these new event handlers, Electron would act just like it does today (silently preventing the unload without any user confirmation).

  7. added a commit that references this issue on May 11, 2017
  8. codebytere commented on Jan 25, 2018

    @codebytere
    Member

    Closing due to merge of #9331

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions