I have this electron.js app and I would like the application to close when I click on the text with class closer
. Here is my code:
HTML
JavaScript
x
23
23
1
<!DOCTYPE html>
2
<html>
3
<head>
4
<meta charset="UTF-8">
5
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
6
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
7
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
8
<title>Hello World!</title>
9
<link rel="stylesheet" href="../style.css">
10
</head>
11
<body>
12
13
14
<div class="content">
15
<h1>Hello</h1>
16
<h1 class="closer">UUUU</h1>
17
</div>
18
19
<script src="./renderer.js"></script>
20
<script src="./closer.js"></script>
21
</body>
22
</html>
23
main.js
initialization electron
JavaScript
1
39
39
1
const {app, BrowserWindow} = require('electron')
2
const path = require('path')
3
4
function createWindow () {
5
// Create the browser window.
6
const mainWindow = new BrowserWindow({
7
width: 1200,
8
height: 600,
9
backgroundColor: "red",
10
frame: false,
11
webPreferences: {
12
nodeIntegration: true,
13
14
preload: path.join(__dirname, 'preload.js')
15
}
16
})
17
18
// and load the index.html of the app.
19
mainWindow.loadFile('index.html')
20
21
22
// mainWindow.webContents.openDevTools()
23
}
24
25
26
app.whenReady().then(() => {
27
createWindow()
28
29
app.on('activate', function () {
30
31
if (BrowserWindow.getAllWindows().length === 0) createWindow()
32
})
33
})
34
35
36
app.on('window-all-closed', function () {
37
if (process.platform !== 'darwin') app.quit()
38
})
39
closer.js
JavaScript
1
11
11
1
const { app } = require('electron');
2
const babi = function(){
3
const bubu = document.querySelector('.closer');
4
function bubub(){
5
bubu.innerHTML = "aganim";
6
app.quit();
7
}
8
bubu.addEventListener('click', bubub);
9
}
10
babi();
11
My problem is that clicking the button, doesn’t work. What should I do?
Advertisement
Answer
JavaScript
1
9
1
[bubu].forEach(bubu =>
2
bubu.addEventListener("click", bubuClose())
3
);
4
5
function bubuClose() {
6
bubu.innerHTML = "aganim";
7
window.close();
8
};
9
I think you can do it with a loop like this.