Skip to content

Behavior of beforeunload event in child windows is different from Chrome #2361

Closed

Description

@etiktin

When you try to close a tab in Google Chrome, the beforeunload event is triggered on the top window and propagates down to the child windows (iframes etc.). If one of the handlers returns a string, the string is displayed to the user in a "confirm navigation" dialog. Once the "confirm navigation" is displayed, the return value of any other handler is ignored in order to prevent abuse (imagine trying to leave a site and getting 10 confirmation messages).

In Electron, the beforeunload event is different. If you return a string, it won't be displayed in a "confirm navigation" dialog. Instead you need to create the dialog manully using dialog.showMessageBox (see #2301 for example). The problem with that is that there's no automatic mechanism to prevent double confirmation messages. So you can show a confirmation message in the top window and choose to stay (return true or a non empty string from the handler), but then the child window handler is called and you are asked again if you want to stay or not (the same happens even if you choose to leave and return false from the handler).

What is the best way to flag that the confirmation message was displayed and there's no reason to show another one during the current event propagation?

There doesn't seem to be anything in the event object that can be used as identifier (each handler gets a different instance).
A possible solution is to save the time stamp when the confirmation is shown and block the following confirmations for a short period of time (maybe 2 seconds). I don't really like this solution because it might block other navigation events (e.g. you decided to "stay" during a navigate, but then changed your mind and decided you want to exit the application, the exit might be blocked). Is there a better way?

Activity

  1. etiktin commented on Aug 1, 2015

    @etiktin
    ContributorAuthor

    I ended up, using a timer to prevent double "confirm navigation" dialogs. The idea is that in each beforeunload handler I check a flag to see if the message was displayed. If it wasn't, I show the message, set the flag and set a timer to remove the flag (using setTimeout with no delay). Because the timer will fire only after the current chain of events, it guarantees that for the remaining beforeunload propagation, the flag will be set and no extra confirmation will be requested.

    I don't know what was the idea behind changing the normal browser behavior of beforeunload, but it seems like a weird design decision (no offence). BrowserWindow already has a close event that you can use to prevent a navigation and display a custom dialog, so why did we need to mess with beforeunload? Replicating the normal behavior of beforeunload, that prevents more than one confirmation per event, is difficult and not straight forward. Granted if you build an entirely new application using Electron, you can work around this, but if you're trying to convert an existing web application it's needless friction.

  2. changed the title [-]How to follow the propagation of the beforeunload event? [/-] [+]Behavior of beforeunload event in child windows is different from Chrome[/+] on Aug 3, 2015
  3. zcbenz commented on Sep 17, 2015

    @zcbenz
    Contributor

    I'm merging this to #2579 since it proposed a fix of this.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions