dki*_*hof 2 javascript confirm onbeforeunload electron
我知道有成百上千个问题,例如:“如何防止电子中的紧密事件”或类似的问题。
在事件中实现确认框(电子消息框)后,beforeunload我能够关闭我的应用并取消关闭事件。由于开发工具始终处于打开状态,因此我不认识到在关闭开发工具时它不起作用...
window.onbeforeunload = e =>
{
// show a message box with "save", "don't save", and "cancel" button
let warning = remote.dialog.showMessageBox(...)
switch(warning)
{
case 0:
console.log("save");
return;
case 1:
console.log("don't save");
return;
case 2:
console.log("cancel");
return false;
// e.returnValue = "false";
// e.returnValue = false;
}
};
Run Code Online (Sandbox Code Playgroud)
因此,当打开开发工具时,我可以保存并关闭应用程序,而无需保存和取消事件。
关闭开发工具后,“取消”按钮不再起作用。
顺便说一句:
window.onbeforeunload = e =>
{
return false;
alert("foo");
};
Run Code Online (Sandbox Code Playgroud)
将取消关闭事件,并且显然不会显示消息(开发工具是打开还是关闭都没关系)
window.onbeforeunload = e =>
{
alert("foo");
return false;
};
Run Code Online (Sandbox Code Playgroud)
如果打开了开发工具,则在按OK后将取消关闭事件;如果关闭了开发工具,则在按OK后将关闭应用程序
我故意使用消息框的同步api,在编写此问题时,我发现两个窗口化的应用程序(new remote.BrowserWindow())的行为与开发工具完全相同。
有谁知道我如何解决这个问题?
提前谢谢了
小智 6
与其onbeforeunload更喜欢与事件合作close。通过此事件,您将能够在整个关闭过程完成之前捕获关闭事件(事件closed)。使用close,您将可以控制并在需要完成关闭操作时停止。
创建浏览器窗口时,最好在主流程中,这是可能的:
// Create the browser window.
window = new BrowserWindow({});
// Event 'close'
window.on('close', (e) => {
// Do your control here
if (bToStop) {
e.preventDefault();
}
}
// Event 'closed'
window.on('closed', (e) => {
// Fired only if you didn't run the line e.preventDefault(); above!
}
Run Code Online (Sandbox Code Playgroud)
此外,请注意该功能e.preventDefault()正在整个代码中扩展。如果您需要恢复电子的自然行为,则需要将变量切换e.defaultPrevented为false。
实际上,似乎e.preventDefault()函数正在处理变量e.defaultPrevented,true直到对其进行任何更改为止。
| 归档时间: |
|
| 查看次数: |
2701 次 |
| 最近记录: |