If you do not want your app to open website links
within the app, you can use the shell
module
to open them externally. When clicked, the links will open outside
of your app and in the user's default web browser.
When the demo button is clicked, the electron website will open in your browser.
const { shell } = require('electron')
const exLinksBtn = document.getElementById('open-ex-links')
exLinksBtn.addEventListener('click', (event) => {
shell.openExternal('https://electronjs.org')
})
You may want to open all http
and
https
links outside of your app. To do this, query
the document and loop through each link and add a listener. This
app uses the code below which is located in
assets/ex-links.js
.
const { shell } = require('electron')
const links = document.querySelectorAll('a[href]')
Array.prototype.forEach.call(links, (link) => {
const url = link.getAttribute('href')
if (url.indexOf('http') === 0) {
link.addEventListener('click', (e) => {
e.preventDefault()
shell.openExternal(url)
})
}})