我正在寫電子應用程序,如果用戶打開未保存的文件,我想在保存之前提示用戶。我發現這個示例代碼在線:電子阻止主窗口關閉
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調用