andersamer andersamer - 5 months ago 119
Javascript Question

Opening external links in Electron with the HTML anchor tag

I'm making an electron app and I want to make a button in my

index.html
file to open up an external link to my GitHub repository website. I have seen other StackOverflow questions on this with successful answers, but they contain snippets of JavaScript and I don't know where to put them.

Answer

When you say "external link" I assume you want to open another window containing your github repo.

This can be done by adding the following code to your main.js:

At its beginning:

const ipcMain = require('electron').ipcMain;

Inside the app.on function:

var externalWindow = new BrowserWindow ({
    width: 800,
    height: 600
})

After the app.on function:

ipcMain.on('load-page', (event, arg) => {
    externalWindow.loadURL(arg);
});

Within the head of your index.html you then need to instantiate the ICP module:

<script>
const ipc = require('electron').ipcRenderer;
</script>

Then use the onclick event in an href to actually perform the loading of the new window:

<a HREF="#" onclick="ipc.send('loadGH','http://github.com/yourGitHubName');">Link</a>