javascript - Electron prevent main window from closing -


i writing application in electron if user has unsaved file open want prompt user before saving it. found example code online:

window.onbeforeunload = (e) => {       var answer = confirm('do want close application?');       e.returnvalue = answer;  // *prevent* closing no matter value passed       if(answer) { mainwindow.destroy(); }  // close app     }; 

this code strangely works if dialogs yes, cancel or x button pressed within few seconds of appearing if let dialog rest on screen little , click button application close no matter pressed.

this code located in main script file called index.html

really strange behavior! cannot explain why it's happening, can give workaround implemented in main process.

you can use electron's dialog module , create same confirmation dialog electron. 1 works expected.

main.js

const { app, browserwindow, dialog } = require('electron') const path = require('path')  app.once('ready', () => {   let win = new browserwindow()   win.loadurl(path.resolve(__dirname, 'index.html'))   win.on('close', e => {     let choice = dialog.showmessagebox(       win,       {         type: 'question',         buttons: ['yes', 'no'],         title: 'confirm',         message: 'do want close application?'       }     )     if (choice === 1) e.preventdefault()   }) }) 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -