2020-11-10 17:06:03 +00:00
|
|
|
# Opening windows from the renderer
|
|
|
|
|
|
|
|
There are several ways to control how windows are created from trusted or
|
|
|
|
untrusted content within a renderer. Windows can be created from the renderer in two ways:
|
|
|
|
|
|
|
|
- clicking on links or submitting forms adorned with `target=_blank`
|
|
|
|
- JavaScript calling `window.open()`
|
|
|
|
|
|
|
|
In non-sandboxed renderers, or when `nativeWindowOpen` is false (the default), this results in the creation of a
|
|
|
|
[`BrowserWindowProxy`](browser-window-proxy.md), a light wrapper around
|
|
|
|
`BrowserWindow`.
|
|
|
|
|
|
|
|
However, when the `sandbox` (or directly, `nativeWindowOpen`) option is set, a
|
|
|
|
`Window` instance is created, as you'd expect in the browser. For same-origin
|
|
|
|
content, the new window is created within the same process, enabling the parent
|
|
|
|
to access the child window directly. This can be very useful for app sub-windows that act
|
|
|
|
as preference panels, or similar, as the parent can render to the sub-window
|
|
|
|
directly, as if it were a `div` in the parent.
|
|
|
|
|
|
|
|
Electron pairs this native Chrome `Window` with a BrowserWindow under the hood.
|
|
|
|
You can take advantage of all the customization available when creating a
|
|
|
|
BrowserWindow in the main process by using `webContents.setWindowOpenHandler()`
|
|
|
|
for renderer-created windows.
|
|
|
|
|
|
|
|
BrowserWindow constructor options are set by, in increasing precedence
|
|
|
|
order: options inherited from the parent, parsed options
|
|
|
|
from the `features` string from `window.open()`, security-related webPreferences
|
|
|
|
inherited from the parent, and options given by
|
|
|
|
[`webContents.setWindowOpenHandler`](web-contents.md#contentssetwindowopenhandler-handler).
|
|
|
|
Note that `webContents.setWindowOpenHandler` has final say and full privilege
|
|
|
|
because it is invoked in the main process.
|
2015-09-22 14:46:44 +00:00
|
|
|
|
2015-08-29 06:21:09 +00:00
|
|
|
### `window.open(url[, frameName][, features])`
|
2015-03-04 16:47:04 +00:00
|
|
|
|
|
|
|
* `url` String
|
2015-08-29 06:21:09 +00:00
|
|
|
* `frameName` String (optional)
|
|
|
|
* `features` String (optional)
|
2015-03-04 16:47:04 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
Returns [`BrowserWindowProxy`](browser-window-proxy.md) | [`Window`](https://developer.mozilla.org/en-US/docs/Web/API/Window)
|
2015-03-04 16:47:04 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
`features` is a comma-separated key-value list, following the standard format of
|
|
|
|
the browser. Electron will parse `BrowserWindowConstructorOptions` out of this
|
|
|
|
list where possible, for convenience. For full control and better ergonomics,
|
|
|
|
consider using `webContents.setWindowOpenHandler` to customize the
|
|
|
|
BrowserWindow creation.
|
|
|
|
|
|
|
|
A subset of `WebPreferences` can be set directly,
|
|
|
|
unnested, from the features string: `zoomFactor`, `nodeIntegration`, `preload`,
|
|
|
|
`javascript`, `contextIsolation`, and `webviewTag`.
|
2018-09-10 15:49:54 +00:00
|
|
|
|
|
|
|
For example:
|
2020-11-05 22:12:43 +00:00
|
|
|
|
2018-09-10 15:49:54 +00:00
|
|
|
```js
|
2020-11-10 17:06:03 +00:00
|
|
|
window.open('https://github.com', '_blank', 'top=500,left=200,frame=false,nodeIntegration=no')
|
2018-09-10 15:49:54 +00:00
|
|
|
```
|
2015-09-22 14:46:44 +00:00
|
|
|
|
2016-09-23 07:08:32 +00:00
|
|
|
**Notes:**
|
2016-10-25 03:35:18 +00:00
|
|
|
|
2016-10-04 05:41:37 +00:00
|
|
|
* Node integration will always be disabled in the opened `window` if it is
|
|
|
|
disabled on the parent window.
|
2017-04-21 18:00:31 +00:00
|
|
|
* Context isolation will always be enabled in the opened `window` if it is
|
|
|
|
enabled on the parent window.
|
|
|
|
* JavaScript will always be disabled in the opened `window` if it is disabled on
|
|
|
|
the parent window.
|
2016-10-04 05:41:37 +00:00
|
|
|
* Non-standard features (that are not handled by Chromium or Electron) given in
|
2020-11-10 17:06:03 +00:00
|
|
|
`features` will be passed to any registered `webContents`'s
|
|
|
|
`did-create-window` event handler in the `additionalFeatures` argument.
|
2015-03-04 16:47:04 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
To customize or cancel the creation of the window, you can optionally set an
|
|
|
|
override handler with `webContents.setWindowOpenHandler()` from the main
|
|
|
|
process. Returning `false` cancels the window, while returning an object sets
|
|
|
|
the `BrowserWindowConstructorOptions` used when creating the window. Note that
|
|
|
|
this is more powerful than passing options through the feature string, as the
|
|
|
|
renderer has more limited privileges in deciding security preferences than the
|
|
|
|
main process.
|
2015-03-04 16:47:04 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
### `BrowserWindowProxy` example
|
2017-03-19 09:35:12 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
```javascript
|
2017-03-19 09:35:12 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
// main.js
|
|
|
|
const mainWindow = new BrowserWindow()
|
2017-03-19 09:35:12 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
if (url.startsWith('https://github.com/')) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
2017-05-24 18:12:57 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
mainWindow.webContents.on('did-create-window', (childWindow) => {
|
|
|
|
// For example...
|
|
|
|
childWindow.webContents('will-navigate', (e) => {
|
|
|
|
e.preventDefault()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
```
|
2017-05-24 18:12:57 +00:00
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
```javascript
|
|
|
|
// renderer.js
|
|
|
|
const windowProxy = window.open('https://github.com/', null, 'minimizable=false')
|
|
|
|
windowProxy.postMessage('hi', '*')
|
2017-05-24 18:12:57 +00:00
|
|
|
```
|
|
|
|
|
2020-11-10 17:06:03 +00:00
|
|
|
### Native `Window` example
|
2017-03-19 09:35:12 +00:00
|
|
|
|
|
|
|
```javascript
|
2020-11-10 17:06:03 +00:00
|
|
|
// main.js
|
2017-03-19 11:30:07 +00:00
|
|
|
const mainWindow = new BrowserWindow({
|
|
|
|
webPreferences: {
|
|
|
|
nativeWindowOpen: true
|
|
|
|
}
|
|
|
|
})
|
2020-11-10 17:06:03 +00:00
|
|
|
|
|
|
|
// In this example, only windows with the `about:blank` url will be created.
|
|
|
|
// All other urls will be blocked.
|
|
|
|
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
|
|
|
|
if (url === 'about:blank') {
|
|
|
|
return {
|
|
|
|
frame: false,
|
|
|
|
fullscreenable: false,
|
|
|
|
backgroundColor: 'black',
|
|
|
|
webPreferences: {
|
|
|
|
preload: 'my-child-window-preload-script.js'
|
|
|
|
}
|
|
|
|
}
|
2017-03-19 09:35:12 +00:00
|
|
|
}
|
2020-11-10 17:06:03 +00:00
|
|
|
return false
|
2017-03-19 11:09:52 +00:00
|
|
|
})
|
2017-03-19 09:35:12 +00:00
|
|
|
```
|
2017-03-19 11:30:07 +00:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
// renderer process (mainWindow)
|
2020-11-10 17:06:03 +00:00
|
|
|
const childWindow = window.open('', 'modal')
|
|
|
|
childWindow.document.write('<h1>Hello</h1>')
|
2017-03-19 11:30:07 +00:00
|
|
|
```
|