Reputation: 382
I have weird problem. In my app there is few links to other .html files. On Mac OS or Windows 7 links are open in the same window but in Windows 10 they open in new window. I don't have idea what is going on... Maybe you know? Link looks like (there is nothing different):
<a href="page.html?id={{ id }}">Some link</a>
Upvotes: 2
Views: 7862
Reputation: 2030
This has since been removed from Electron see: https://www.electronjs.org/docs/latest/breaking-changes#planned-breaking-api-changes-220.
You are now ment to use https://www.electronjs.org/docs/latest/api/web-contents#contentssetwindowopenhandlerhandler.
mainWindow.webContents.setWindowOpenHandler((details) => {
return { action: 'deny' }
});
Upvotes: 1
Reputation: 1164
I was facing the same issue with an Electron / Vue.js app running on a touch-screen Linux device, where touching a link would sometimes open it in a new window. Jann3's answer works, but has the effect of reloading the Vue app which introduces a lot of lag.
I found an Electron workaround by simply emitting a message from Electron's main process to the renderer process, in this case using Vue's router. The same logic should be applicable whatever you're using in the renderer process:
Electron main process:
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] || '/'})
})
Here is more on sending messages between the Main and Renderer processes: https://electronjs.org/docs/api/web-contents#contentssendchannel-arg1-arg2-
Upvotes: 2
Reputation: 111
I'm not sure whats going on in your app to open new windows, so this doesn't get to the cause of your problem.
However, assuming your windows are being created by a rogue window.open call (or a target="_blank") you could block new-window events and load in your win like the following:
win.webContents.on('new-window', (event, url) => {
event.preventDefault()
win.loadURL(url)
})
Upvotes: 11