admin管理员组

文章数量:1022956

I put a webapp into ElectronJS framework. In the webapp (actually webpage) there are some links target=_blank. However when I click the link, it will open another Electron browser window. Expectedly I want the link to be shown in default web browser.

What I can think about is to trap such navigation event and then send a message to the main browser window and let it spawn a default browser instance to show the link.

I think this is a bit plicated. I'm seeking an easier way to do this.

I put a webapp into ElectronJS framework. In the webapp (actually webpage) there are some links target=_blank. However when I click the link, it will open another Electron browser window. Expectedly I want the link to be shown in default web browser.

What I can think about is to trap such navigation event and then send a message to the main browser window and let it spawn a default browser instance to show the link.

I think this is a bit plicated. I'm seeking an easier way to do this.

Share Improve this question asked Nov 2, 2016 at 0:49 stanleyxu2005stanleyxu2005 8,27115 gold badges61 silver badges99 bronze badges 1
  • Does this answer your question? How can I force external links from browser-window to open in a default browser from Electron? – icc97 Commented Aug 24, 2023 at 13:19
Add a ment  | 

1 Answer 1

Reset to default 9

You've got the right idea, you need to listen to the new-window event and forward the URL to the default browser. You'd implement it in the main process like so:

import { shell } from 'electron'

mainWindow.webContents.on('new-window', (event, url) => {
  // stop Electron from opening another BrowserWindow
  event.preventDefault()
  // open the url in the default system browser
  shell.openExternal(url)
})

Note that the new-window event actually provides some additional information about the window to be opened (not just the URL), it may make sense for you to take some of that info into account to figure out if the URL should be forwarded to the default browser or not.

I put a webapp into ElectronJS framework. In the webapp (actually webpage) there are some links target=_blank. However when I click the link, it will open another Electron browser window. Expectedly I want the link to be shown in default web browser.

What I can think about is to trap such navigation event and then send a message to the main browser window and let it spawn a default browser instance to show the link.

I think this is a bit plicated. I'm seeking an easier way to do this.

I put a webapp into ElectronJS framework. In the webapp (actually webpage) there are some links target=_blank. However when I click the link, it will open another Electron browser window. Expectedly I want the link to be shown in default web browser.

What I can think about is to trap such navigation event and then send a message to the main browser window and let it spawn a default browser instance to show the link.

I think this is a bit plicated. I'm seeking an easier way to do this.

Share Improve this question asked Nov 2, 2016 at 0:49 stanleyxu2005stanleyxu2005 8,27115 gold badges61 silver badges99 bronze badges 1
  • Does this answer your question? How can I force external links from browser-window to open in a default browser from Electron? – icc97 Commented Aug 24, 2023 at 13:19
Add a ment  | 

1 Answer 1

Reset to default 9

You've got the right idea, you need to listen to the new-window event and forward the URL to the default browser. You'd implement it in the main process like so:

import { shell } from 'electron'

mainWindow.webContents.on('new-window', (event, url) => {
  // stop Electron from opening another BrowserWindow
  event.preventDefault()
  // open the url in the default system browser
  shell.openExternal(url)
})

Note that the new-window event actually provides some additional information about the window to be opened (not just the URL), it may make sense for you to take some of that info into account to figure out if the URL should be forwarded to the default browser or not.

本文标签: javascriptForce open link in external browser in ElectronJS applicationStack Overflow