How can I force external links from browser-window to open in a default browser from Electron?

Lipis picture Lipis · Sep 4, 2015 · Viewed 17k times · Source

I'm using the BrowserWindow to display an app and I would like to force the external links to be opened in the default browser. Is that even possible or I have to approach this differently?

Answer

René Herrmann picture René Herrmann · Sep 6, 2015

I came up with this, after checking the solution from the previous answer.

mainWindow.webContents.on('new-window', function(e, url) {
  e.preventDefault();
  require('electron').shell.openExternal(url);
});

According to the electron spec, new-window is fired when external links are clicked.

NOTE: Requires that you use target="_blank" on your anchor tags.