I am completely new to electron.js and I was just trying to load a new page(calling it index2.html) in the same BrowserWindow(which has already loaded index1.html) on a button click.The way I am trying to accomplish this is when a button is clicked, the rendering process corresponsing to the BrowserWindow sends an ipc event to the main Process, the main process then loads a new html into the BrowserWindow by calling loadFile(). The issue I am facing here is after the button click, the new page does not load. I have verified that the ipc event is being caught by the main process using console print. Also, other actions on the window like window minimize are working. Can somebody help me understand where I am going wrong?
This is my directory structure
package.json
{
"name": "hello-world",
"version": "1.0.0",
"main": "main.js",
"devDependencies": {
"electron": "^9.2.0"
},
"scripts":{
"start":"electron ."
}
}
main.js
console.log("Main process running");
const electron = require("electron");
const app = electron.app ;
const BrowserWindow = electron.BrowserWindow;
const path = require("path");
const url = require("url");
const ipc = electron.ipcMain
let win;
function createWindow()
{
win = new BrowserWindow({
webPreferences: {nodeIntegration: true},
show:false
});
win.loadFile('src/html/index.html');
win.on('ready-to-show',()=>{
win.show();
})
;
}
ipc.on('login-success',function(event,arg){
console.log("Hello World");//Making sure that main process catches the event
//load new url
win.loadFile('src/html/index2.html');//does nothing
/*
the following command works though
win.minimize();
*/
});
app.on('ready',createWindow);
index.html
<html>
<head>
<link rel="stylesheet" href="../css/index.css">
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>ChatIn</title>
</head>
<body>
<div class="main">
<p class="sign" align="center">Sign in</p>
<form class="form1">
<input class="un " type="text" align="center" placeholder="Username">
<input class="pass" type="password" align="center" placeholder="Password">
<button class="submit" align="center">Sign in</button>
<p class="forgot" align="center"><a href="#">Forgot Password?</p>
<script>
require("../js/login.js")
</script>
</div>
</body>
</html>
login.js
const electron = require("electron");
const ipc = electron.ipcRenderer;
const btnArray = document.getElementsByClassName("submit");
const loginBtn = btnArray[0];
loginBtn.addEventListener('click',(event)=>{
ipc.send('login-success');
},false);
index2.html (I don`t think it will affect the problem in hand but still here it is)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Welcome</title>
<link rel="stylesheet" href="../css/index2.css">
</head>
<body>
<section class="msger">
<header class="msger-header">
<div class="msger-header-title">
<i class="fas fa-comment-alt"></i> SimpleChat
</div>
<div class="msger-header-options">
<span><i class="fas fa-cog"></i></span>
</div>
</header>
<main class="msger-chat">
<div class="msg left-msg">
<div
class="msg-img"
style="background-image: url(https://image.flaticon.com/icons/svg/327/327779.svg)"
></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">BOT</div>
<div class="msg-info-time">12:45</div>
</div>
<div class="msg-text">
Hi, welcome to SimpleChat! Go ahead and send me a message.
</div>
</div>
</div>
<div class="msg right-msg">
<div
class="msg-img"
style="background-image: url(https://image.flaticon.com/icons/svg/145/145867.svg)"
></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">Sajad</div>
<div class="msg-info-time">12:46</div>
</div>
<div class="msg-text">
You can change your name in JS section!
</div>
</div>
</div>
</main>
<form class="msger-inputarea">
<input type="text" class="msger-input" placeholder="Enter your message...">
<button type="submit" class="msger-send-btn">Send</button>
</form>
</section>
</body>
</html>
Things I tried/noticed
- Using loadURL instead of loadFile does not help. Trying to load a website instead of a local file also does not work using the method above.
- Using
win=nullin the main ipc block does not close the browserwindow, butwin.close()orwin.destroy()works, no idea why. - Using var instead of let for win variable does not help.
- Loading the new html into a new browserwindow and closing the previous browserwindow does the trick but still does not feel like the most elegant way to get the job done.