如何强制浏览器窗口的外部链接在 Electron 的默认浏览器中打开? [英] How can I force external links from browser-window to open in a default browser from Electron?

查看:30
本文介绍了如何强制浏览器窗口的外部链接在 Electron 的默认浏览器中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 BrowserWindow 来显示应用程序和我想强制在默认浏览器中打开外部链接.这是否可能,或者我必须以不同的方式处理这个问题?

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?

推荐答案

在检查了上一个答案的解决方案后,我想出了这个.

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);
});

根据 电子spec, new-window 在点击外部链接时被触发.

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

注意:要求您在锚标记上使用 target="_blank".

这篇关于如何强制浏览器窗口的外部链接在 Electron 的默认浏览器中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆