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
window.addEventListener('beforeunload', onbeforeunload) can't prevent the window from being closed #2481
window.onbeforeunload = onbeforeunload; works as expected however.
Tested with Electron 0.30.4 win32.
By the way, why beforeunload event works differently in Electron? If you absolutely can't make it spec-compliant, I suggest making a new Electron-specific event, and make beforeunload event a no-op.
This happens also in v0.30.2 on Windows 8.1 64 bit.
I tried inspecting the beforeunload listeners using getEventListeners(window) and everything looks fine. Also tried using Object.getOwnPropertyDescriptor(window, 'onbeforeunload'), to see if there's a special setter for onbeforeunload but there isn't.
I agree that modifying the behavior of beforeunload is confusing (see #2361 for additional issues).
If the user did not confirm the page navigation, then the user agent refused to allow the document to be unloaded.
Since the developers are free to create their own confirmation dialogs in Electron, we require the developers to return the result for the required action to proceed. Maybe this should be documented ?
If the returnValue attribute of the event object is not the empty string, or if the event was canceled, then the user agent should ask the user to confirm that they wish to unload the document.
So e.returnValue = "false" should result in a confirm dialog per spec; however Electron simply rejects the navigation/closing in this case.
So I suggest adding a Electron specific event, e.g. electron-beforeunload to allow custom dialogs, and make beforeunload spec-compaliant.
Also note that this issue is mainly about the different behaviors of window.onbeforeunload and window.addEventListener('beforeunload',) in Electron.
Proiding a new event doesnt sound good, if event.returnValue is something other than empty string or false Electron can fallback to provide its own dialog for confirmation. Does that sound good ? will wait for others thoughts.
@deepak1556, in your example you wrote e.returnValue = "false";, didn't you mean e.returnValue = false;?
Why does returning false from window.onbeforeunload works, but we need to use e.returnValue from handlers added through window.addEventListener? I have no problem using e.returnValue, but I'm just curious why it works differently.
@duanyao, BrowserWindow already has a special event that allows you to prevent unload. See the close event. That's why it's weird that we also mess with the normal behavior of beforeunload.
Regarding the following:
providing a new event doesn't sound good, if event.returnValue is something other than empty string or false Electron can fallback to provide its own dialog for confirmation.
I would prefer that beforeunload will act exactly like it does in browsers. If we can't do that because it's a breaking change (although I think most developers will be for it), then yes I would go for what you're suggesting, but we need to make sure that the fallback confirmation dialog appears only once per propagation (see #2361 for details).
I think it would be better if customization of dialogs (confirm navigation, alert, confirm and prompt), would be done by registering a handler. So for example if I want my alerts to have a custom title and icon, I could do something like this:
@deepak1556 I'm not sure how fallback would work in your mind. Currently I have to do some UA-sniff:
function onBeforeUnload(ev) {
var msg = '...';
if (!config.isElectron) {
ev.returnValue = msg;
} else {
return confirm(msg);
}
}
@etiktin It seems close event is unavailable to web content. e.returnValue = false is equivlant to e.returnValue = "false", because BeforeUnloadEvent.returnValue is a string, and false is converted to string "false".
I agree that beforeunload should be web-compatible unless you have very good reason not to.
Customization of dialogs globally sounds a good idea. In fact gecko/xulrunner allow you do this, take the geckofx code as an example.
@duanyao you shldnt need to UA sniff with the fallback, my suggestion was
ev.returnValue = '' // empty string run silently
ev.returnValue = 'false' // result from custom dialog to cancel
ev.returnValue = msg // create dailog with message for conformation
not favouring the false value, just dont want to break existing users using custom dialogs.
function onBeforeUnload(ev) {
var msg = '...';
ev.returnValue = msg; // non-empty string create a dialog with message for confirmation
}
Having said that, a better approach would be to set custom dialogs via api as callback to AtomJavaScriptDialogManager as noted by @etiktin#2522 and use them or default to native, that way can be made web-compatible. If any of you would like a PR the required logic is here
For the difference between window.addEventListener and window.onbeforeunload, it is not an Electron thing, it is actually designed by DOM and you can see the same behavior in every modern browser.
Allowing registering custom handler for the beforeunload dialog doesn't solve the problem completely, because the custom handler will run in the main process while all your data is in the renderer process. So can we make Electron run the custom handler in renderer process to solve it? No, it will just create a dead lock since the renderer process is already waiting for the main process to answer.
Making use of unload handler is not an options too, because there is an optimization in Chrome that will delay the call of unload handler after loading the new page.
For changing the behavior of return false in beforeunload handler, in normal browser returning false is the same with returning "false", because the beforeunload handler converts all results to string type. In electron we treated false and "false" specially because many APIs just return boolean type and this makes things simpler. Is this a good behavior? Probably not. But it is too late to change that since lots of apps are already relying on it.
We can do breaking changes, but existing code must break very noisily, if we do a breaking change silently it would just result in lots of developers spending hours trying to find why their apps stop working.
However, for the behavior of beforeunload handler in Electron, I think the problem is that Electron already breaks existing web pages. If we assume that all codes served to Electron are designed with Electron in mind, then it is fine. But this is not the case. What if one wants to build an app that may load arbitray web pages (My current project is one example)?
I think the reasonable trade-off has already been proposed by @deepak1556, let me make it clearer:
ev.returnValue='false'// do what Electron does currentlyreturn'false';// do what Electron does currentlyev.returnValue=a_string_other_than_false;// do what the spec saysreturna_string_other_than_false;// do what the spec says
@duanyao I think we should also handle the case of returning true, so for apps that returning booleans the behavior is not changed. Other parts of the proposal looks good to me, can you create a new issue for that?
Activity
etiktin commentedon Aug 12, 2015
This happens also in v0.30.2 on Windows 8.1 64 bit.
I tried inspecting the
beforeunloadlisteners usinggetEventListeners(window)and everything looks fine. Also tried usingObject.getOwnPropertyDescriptor(window, 'onbeforeunload'), to see if there's a special setter foronbeforeunloadbut there isn't.I agree that modifying the behavior of
beforeunloadis confusing (see #2361 for additional issues).deepak1556 commentedon Aug 18, 2015
@duanyao you need to set the return value of event object in
beforeunloadhandler tofalseoremptystring inorder to prevent navigation.The spec states
Since the developers are free to create their own confirmation dialogs in Electron, we require the developers to return the result for the required action to proceed. Maybe this should be documented ?
duanyao commentedon Aug 18, 2015
@deepak1556
The spec says:
So
e.returnValue = "false"should result in a confirm dialog per spec; however Electron simply rejects the navigation/closing in this case.So I suggest adding a Electron specific event, e.g.
electron-beforeunloadto allow custom dialogs, and makebeforeunloadspec-compaliant.Also note that this issue is mainly about the different behaviors of
window.onbeforeunloadandwindow.addEventListener('beforeunload',)in Electron.deepak1556 commentedon Aug 18, 2015
Proiding a new event doesnt sound good, if
event.returnValueis something other than empty string orfalseElectron can fallback to provide its own dialog for confirmation. Does that sound good ? will wait for others thoughts.etiktin commentedon Aug 18, 2015
@deepak1556, in your example you wrote
e.returnValue = "false";, didn't you meane.returnValue = false;?Why does returning
falsefromwindow.onbeforeunloadworks, but we need to usee.returnValuefrom handlers added throughwindow.addEventListener? I have no problem usinge.returnValue, but I'm just curious why it works differently.@duanyao,
BrowserWindowalready has a special event that allows you to prevent unload. See the close event. That's why it's weird that we also mess with the normal behavior ofbeforeunload.Regarding the following:
I would prefer that
beforeunloadwill act exactly like it does in browsers. If we can't do that because it's a breaking change (although I think most developers will be for it), then yes I would go for what you're suggesting, but we need to make sure that the fallback confirmation dialog appears only once per propagation (see #2361 for details).I think it would be better if customization of dialogs (confirm navigation, alert, confirm and prompt), would be done by registering a handler. So for example if I want my alerts to have a custom title and icon, I could do something like this:
Update browser-window.md
duanyao commentedon Aug 18, 2015
@deepak1556 I'm not sure how fallback would work in your mind. Currently I have to do some UA-sniff:
@etiktin It seems
closeevent is unavailable to web content.e.returnValue = falseis equivlant toe.returnValue = "false", becauseBeforeUnloadEvent.returnValueis a string, andfalseis converted to string "false".I agree that
beforeunloadshould be web-compatible unless you have very good reason not to.Customization of dialogs globally sounds a good idea. In fact gecko/xulrunner allow you do this, take the geckofx code as an example.
deepak1556 commentedon Aug 18, 2015
@duanyao you shldnt need to UA sniff with the fallback, my suggestion was
not favouring the false value, just dont want to break existing users using custom dialogs.
Having said that, a better approach would be to set custom dialogs via api as callback to
AtomJavaScriptDialogManageras noted by @etiktin #2522 and use them or default to native, that way can be made web-compatible. If any of you would like a PR the required logic is hereduanyao commentedon Aug 19, 2015
@deepak1556 Thanks for explanation. I think it is acceptable to just reserve 'false' for Electron apps.
zcbenz commentedon Aug 25, 2015
For the difference between
window.addEventListenerandwindow.onbeforeunload, it is not an Electron thing, it is actually designed by DOM and you can see the same behavior in every modern browser.For the behavior of
beforeunloadhandler, showing a confirmation dialog doesn't cover all use cases. Suppose you are writing a text editor, before you close or refresh the window you need to show a dialog to ask the users whether to save the file, and if they choose "Save" you also need to show another dialog to ask where to save the file, and finally do some filesystem operations, and it can not be solved by showing a confirmation dialog. And example is what Atom did:https://github.com/atom/atom/blob/a13e0020796b323e3e6c85bfeb873dfb81e0a779/src/window-event-handler.coffee#L60
Allowing registering custom handler for the
beforeunloaddialog doesn't solve the problem completely, because the custom handler will run in the main process while all your data is in the renderer process. So can we make Electron run the custom handler in renderer process to solve it? No, it will just create a dead lock since the renderer process is already waiting for the main process to answer.Making use of
unloadhandler is not an options too, because there is an optimization in Chrome that will delay the call ofunloadhandler after loading the new page.For changing the behavior of return
falseinbeforeunloadhandler, in normal browser returningfalseis the same with returning"false", because thebeforeunloadhandler converts all results to string type. In electron we treatedfalseand"false"specially because many APIs just return boolean type and this makes things simpler. Is this a good behavior? Probably not. But it is too late to change that since lots of apps are already relying on it.We can do breaking changes, but existing code must break very noisily, if we do a breaking change silently it would just result in lots of developers spending hours trying to find why their apps stop working.
duanyao commentedon Aug 25, 2015
@zcbenz
Wow, you are correct about the difference between
window.addEventListener('beforeunload')andwindow.onbeforeunload,OnBeforeUnloadEventHandlerhas a special treatment in the spec:https://html.spec.whatwg.org/#the-event-handler-processing-algorithm
However, for the behavior of
beforeunloadhandler in Electron, I think the problem is that Electron already breaks existing web pages. If we assume that all codes served to Electron are designed with Electron in mind, then it is fine. But this is not the case. What if one wants to build an app that may load arbitray web pages (My current project is one example)?I think the reasonable trade-off has already been proposed by @deepak1556, let me make it clearer:
What do you think?
zcbenz commentedon Aug 25, 2015
@duanyao I think we should also handle the case of returning
true, so for apps that returning booleans the behavior is not changed. Other parts of the proposal looks good to me, can you create a new issue for that?duanyao commentedon Aug 25, 2015
@zcbenz Done with #2579.