我正在写电子应用程序,如果用户打开未保存的文件,我想在保存之前提示用户。我发现这个示例代码在线:电子阻止主窗口关闭
window.onbeforeunload = (e) => {
var answer = confirm('Do you really want to close the application?');
e.returnValue = answer; // this will *prevent* the closing no matter what value is passed
if(answer) { mainWindow.destroy(); } // this will close the app
};
如果对话框是,取消或X按钮被按下时出现的几秒钟内,但如果你让屏幕上的对话框休息了一点,然后点击此代码奇怪的工作无论按下什么按钮,应用程序都将关闭。
此代码位于我的主要脚本文件index.html调用