You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Make beforeunload event (largely) web-compatible while maintain backward compatibility #2579
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 emptywindow.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?
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).
@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.
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
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')letwin=newBrowserWindow()win.webContents.on('will-prevent-unload',(event)=>{letchoice=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})letleave=(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).
Activity
etiktin commentedon Aug 25, 2015
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: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).
duanyao commentedon Aug 26, 2015
@etiktin Firefox doesn't show the string of
BeforeUnloadEvent.returnValueat 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 ofBeforeUnloadEvent.preventDefault().duanyao commentedon Aug 26, 2015
@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.
etiktin commentedon Aug 26, 2015
@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 navigationtwolfson commentedon Jan 22, 2017
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:
Make JupyterLab page actually work in browser window
22222 commentedon Apr 28, 2017
It looks like things have changed since this ticket was created. The message returned by the
beforeunloadevent 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-unloadevent forBrowserWindow.webContents, where you can callevent.preventDefault()to let the user leave the page even if abeforeunloadevent tried to cancel the page unload.With that event, implementing a confirmation like in Chromium would look like this:
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).
Add 'will-prevent-unload' event for electron#2579.
Merge pull request #9331 from 22222/issue2579
codebytere commentedon Jan 25, 2018
Closing due to merge of #9331