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
How to display confirmation dialog on beforeunload? #2301
The beforeunload event in Electron, acts differently than in the browser. If I return a string it's not displayed in a confirmation dialog. I saw that in BrowserWindow you give an example of using beforeunload and in it you mention in a comment that it's possible to display a confirmation dialog:
// You can also use the dialog API to let the user confirm closing the application
Isn't the dialog API available only for the main process?
What's the simplest way to replicate the normal browser behavior?
window.onbeforeunload=function(e){varremote=require('remote');vardialog=remote.require('dialog');varchoice=dialog.showMessageBox(remote.getCurrentWindow(),{type: 'question',buttons: ['Yes','No'],title: 'Confirm',message: 'Are you sure you want to quit?'});returnchoice===0;};
Reacted by HebaruSan, Vijay Pushkin, Richard Caceres, Ethan and Stephan Zuidberg
@shneorasa I know it's been a few months since you asked (and you've probably found a solution by now), but for those looking for a solution in the future, here's a better way in handling this issue as opposed to using window.onbeforeload.
The beforeunload event getting overwritten can potentially mess up future dialogs, breaking your app. Instead, you can store a simple boolean inside your app like I have done:
With that, you can interrupt the close event of your BrowserWindow as follows:
// Inside main/index.js, where BrowserWindow is initializedmainWindow.on('close',(e)=>{if(app.showExitPrompt){e.preventDefault()// Prevents the window from closing dialog.showMessageBox({type: 'question',buttons: ['Yes','No'],title: 'Confirm',message: 'Unsaved data will be lost. Are you sure you want to quit?'},function(response){if(response===0){// Runs the following if 'Yes' is clickedapp.showExitPrompt=falsemainWindow.close()}})}})
I have implemented this solution into an Electron app using Vue.js, and showing/hiding the dialog is as simple as importing app:
const{ app }=require('electron').remote
and overwriting the stored boolean value:
app.showExitPrompt=true
Reacted by Mathias Gorenflot, Devine Lu Linvega, ChesuCR, Donovan Charpin, Hoang Trinh, Stefan Siegl, Lucas Bento, Brett Stevenson, IS-Kuan, Anoop Kumar and 18 more
Reacted by Michael Smith, Stanimir and Edimário Gomes
Reacted by IS-Kuan, Prosen Ghosh, Stanimir and Edimário Gomes
Activity
etiktin commentedon Jul 22, 2015
Never mind. I figured it out.
For future reference, here's an example:
shneorasa commentedon Dec 14, 2016
hi(:
the solution stated above doesn't work for me.
because it still take the "onbeforeunload" of the browser...
any idea?
Abbondanzo commentedon Jun 14, 2017
@shneorasa I know it's been a few months since you asked (and you've probably found a solution by now), but for those looking for a solution in the future, here's a better way in handling this issue as opposed to using
window.onbeforeload.The
beforeunloadevent getting overwritten can potentially mess up future dialogs, breaking your app. Instead, you can store a simple boolean inside your app like I have done:With that, you can interrupt the
closeevent of yourBrowserWindowas follows:I have implemented this solution into an Electron app using Vue.js, and showing/hiding the dialog is as simple as importing
app:and overwriting the stored boolean value: