1. 页面既无法关闭,也无法刷新
window.onbeforeunload = (e) => {
console.log('I do not want to be closed')
// Unlike usual browsers that a message box will be prompted to users, returning
// a non-void value will silently cancel the close.
// It is recommended to use the dialog API to let the user confirm closing the
// application.
e.returnValue = false
}
2. 页面无法关闭
let canQuit = false;
mainWindow.on('close', (event) => {
if (!canQuit) {
event.preventDefault();}
});
文章介绍了JavaScript如何阻止页面在没有用户确认的情况下关闭,通过`window.onbeforeunload`事件和`event.preventDefault()`方法,以及为何推荐使用对话框API来确保用户知情并确认操作。
259

被折叠的 条评论
为什么被折叠?



