电子链接在新窗口中打开

时间:2017-09-28 06:21:49

标签: hyperlink windows-10 electron

我有奇怪的问题。在我的应用程序中,很少有其他.html文件的链接。在Mac OS或Windows 7上,链接在同一窗口中打开,但在Windows 10中,它们在新窗口中打开。我不知道发生了什么......也许你知道吗? 链接看起来像(没有什么不同):

<a href="page.html?id={{ id }}">Some link</a>

2 个答案:

答案 0 :(得分:3)

我不确定您的应用中是否会打开新窗口,所以这并不能解决您的问题。

但是,假设您的窗口是由流氓window.open调用(或目标=&#34; _blank&#34;)创建的,您可以阻止新窗口事件并加载您的胜利,如下所示:

win.webContents.on('new-window', (event, url) => {
    event.preventDefault()
    win.loadURL(url)
})

请参阅more details here

答案 1 :(得分:0)

我在运行于触摸屏Linux设备上的Electron / Vue.js应用程序遇到了同样的问题,触摸某个链接有时会在新窗口中打开它。 Jann3的答案有效,但具有重新加载Vue应用程序的效果,这会带来很多延迟。

我发现了Electron的解决方法,方法是简单地从Electron的主进程向渲染器进程发出一条消息,在这种情况下,使用Vue的路由器。无论在渲染器过程中使用什么,相同的逻辑都应该适用:

电子主过程:

win = new BrowserWindow({
  ...
})

win.webContents.on('new-window', (event, url) => {
  event.preventDefault()
  win.webContents.send('blocked-new-window', url)
})

Vue router.js:

require('electron').ipcRenderer.on('blocked-new-window', (event, url) => {
   router.push({path: url.split('#/')[1] || '/'})
})

关于在主进程和渲染器进程之间发送消息的更多信息:https://electronjs.org/docs/api/web-contents#contentssendchannel-arg1-arg2-

相关问题