admin管理员组文章数量:1023876
I have an electron app that loads the URL of the web version of the app. However I cannot close the application by clicking in the X button and I dont have access to the webapp. I have tried this:
let count = BrowserWindow.getAllWindows().length;
///returns 1, so there is only 1 window)
window.onbeforeunload=function(e){
e.returnValue=undefined;
return undefined;
}
window.close();
app.quit();
Nothing happens.
app.exit(0)
works, but I dont want to use it. Is there a way I can close the app with window.close
and app.quit
?
EDIT: The problem are those global beforeunload events. If i click remove in the devtools I am able to close the app.
Also this let names= window.eventNames();
returns an array that does not have any beforeunload events
I have an electron app that loads the URL of the web version of the app. However I cannot close the application by clicking in the X button and I dont have access to the webapp. I have tried this:
let count = BrowserWindow.getAllWindows().length;
///returns 1, so there is only 1 window)
window.onbeforeunload=function(e){
e.returnValue=undefined;
return undefined;
}
window.close();
app.quit();
Nothing happens.
app.exit(0)
works, but I dont want to use it. Is there a way I can close the app with window.close
and app.quit
?
EDIT: The problem are those global beforeunload events. If i click remove in the devtools I am able to close the app.
Also this let names= window.eventNames();
returns an array that does not have any beforeunload events
- Wait, are you using window global (renderer) inside electron (main) code? – Lukas Bobinas Commented Jul 10, 2019 at 19:29
- can you just post the repo link? – Lukas Bobinas Commented Jul 10, 2019 at 19:33
- I am creating a BrowserWindow and then loading a url in that window. I cant post the link :( – kkica Commented Jul 10, 2019 at 19:38
-
1
I'm no JavaScript hero but I think using
window
as a variable name is a "bad idea". – spring Commented Jul 11, 2019 at 1:54
2 Answers
Reset to default 1In the file where you created the BrowserWindow, you can attach an event to the 'close' handler for the window. For Electron, this is usually main.js.
const {app, BrowserWindow} = require('electron');
let mainWindow = null;
app.on('ready', () => {
mainWindow = new BrowserWindow({
//options here
});
mainWindow.loadURL([url here]);
//attach event handler here
mainWindow.on("close", () => {
mainWindow = null;
app.quit();
});
});
For good measure, in the case that you may have more than one window, place this in your main.js file.
app.on('window-all-closed', () => {
if(process.platform !== "darwin")
app.quit();
});
You need to save the BrowserWindow object reference. Then listen for a 'closed' event and dereference it:
const { app, BrowserWindow } = require('electron');
let win; // = new BrowserWindow();
win.on('closed', () => {
win = null
})
After dereference, it's all simple, just listen for 'window-all-closed' event on app:
app.on('window-all-closed', () => {
app.quit()
})
I have an electron app that loads the URL of the web version of the app. However I cannot close the application by clicking in the X button and I dont have access to the webapp. I have tried this:
let count = BrowserWindow.getAllWindows().length;
///returns 1, so there is only 1 window)
window.onbeforeunload=function(e){
e.returnValue=undefined;
return undefined;
}
window.close();
app.quit();
Nothing happens.
app.exit(0)
works, but I dont want to use it. Is there a way I can close the app with window.close
and app.quit
?
EDIT: The problem are those global beforeunload events. If i click remove in the devtools I am able to close the app.
Also this let names= window.eventNames();
returns an array that does not have any beforeunload events
I have an electron app that loads the URL of the web version of the app. However I cannot close the application by clicking in the X button and I dont have access to the webapp. I have tried this:
let count = BrowserWindow.getAllWindows().length;
///returns 1, so there is only 1 window)
window.onbeforeunload=function(e){
e.returnValue=undefined;
return undefined;
}
window.close();
app.quit();
Nothing happens.
app.exit(0)
works, but I dont want to use it. Is there a way I can close the app with window.close
and app.quit
?
EDIT: The problem are those global beforeunload events. If i click remove in the devtools I am able to close the app.
Also this let names= window.eventNames();
returns an array that does not have any beforeunload events
- Wait, are you using window global (renderer) inside electron (main) code? – Lukas Bobinas Commented Jul 10, 2019 at 19:29
- can you just post the repo link? – Lukas Bobinas Commented Jul 10, 2019 at 19:33
- I am creating a BrowserWindow and then loading a url in that window. I cant post the link :( – kkica Commented Jul 10, 2019 at 19:38
-
1
I'm no JavaScript hero but I think using
window
as a variable name is a "bad idea". – spring Commented Jul 11, 2019 at 1:54
2 Answers
Reset to default 1In the file where you created the BrowserWindow, you can attach an event to the 'close' handler for the window. For Electron, this is usually main.js.
const {app, BrowserWindow} = require('electron');
let mainWindow = null;
app.on('ready', () => {
mainWindow = new BrowserWindow({
//options here
});
mainWindow.loadURL([url here]);
//attach event handler here
mainWindow.on("close", () => {
mainWindow = null;
app.quit();
});
});
For good measure, in the case that you may have more than one window, place this in your main.js file.
app.on('window-all-closed', () => {
if(process.platform !== "darwin")
app.quit();
});
You need to save the BrowserWindow object reference. Then listen for a 'closed' event and dereference it:
const { app, BrowserWindow } = require('electron');
let win; // = new BrowserWindow();
win.on('closed', () => {
win = null
})
After dereference, it's all simple, just listen for 'window-all-closed' event on app:
app.on('window-all-closed', () => {
app.quit()
})
本文标签: javascriptHow to force the closing of an Electron appStack Overflow
版权声明:本文标题:javascript - How to force the closing of an Electron app? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745528472a2154638.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论