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:
2020-11-23 17:15:27 +00:00
* clicking on links or submitting forms adorned with `target=_blank`
* JavaScript calling `window.open()`
2020-11-10 17:06:03 +00:00
2021-04-13 19:36:38 +00:00
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. This is the same behavior as in the browser.
2020-11-10 17:06:03 +00:00
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
2021-04-21 17:55:17 +00:00
order: parsed options from the `features` string from `window.open()` ,
security-related webPreferences inherited from the parent, and options given by
2020-11-19 07:06:32 +00:00
[`webContents.setWindowOpenHandler` ](web-contents.md#contentssetwindowopenhandlerhandler ).
2020-11-10 17:06:03 +00:00
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
2021-11-16 04:13:18 +00:00
* `url` string
* `frameName` string (optional)
* `features` string (optional)
2015-03-04 16:47:04 +00:00
2022-01-06 17:28:03 +00:00
Returns [`Window` ](https://developer.mozilla.org/en-US/docs/Web/API/Window ) | null
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
2023-04-25 13:16:11 +00:00
the browser. Electron will parse [`BrowserWindowConstructorOptions` ](structures/browser-window-options.md ) out of this
2020-11-10 17:06:03 +00:00
list where possible, for convenience. For full control and better ergonomics,
consider using `webContents.setWindowOpenHandler` to customize the
BrowserWindow creation.
2023-04-25 13:16:11 +00:00
A subset of [`WebPreferences` ](structures/web-preferences.md ) can be set directly,
2020-11-10 17:06:03 +00:00
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
2021-04-19 22:46:54 +00:00
`did-create-window` event handler in the `options` argument.
2023-07-24 10:33:41 +00:00
* `frameName` follows the specification of `target` located in the [native documentation ](https://developer.mozilla.org/en-US/docs/Web/API/Window/open#parameters ).
2023-04-25 13:16:11 +00:00
* When opening `about:blank` , the child window's [`WebPreferences` ](structures/web-preferences.md ) will be copied
2021-12-06 03:54:14 +00:00
from the parent window, and there is no way to override it because Chromium
skips browser side navigation in this case.
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
2021-04-13 19:36:38 +00:00
process. Returning `{ action: 'deny' }` cancels the window. Returning `{
action: 'allow', overrideBrowserWindowOptions: { ... } }` will allow opening
2023-04-25 13:16:11 +00:00
the window and setting the [`BrowserWindowConstructorOptions` ](structures/browser-window-options.md ) to be used when
2021-04-13 19:36:38 +00:00
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.
2017-05-24 18:12:57 +00:00
2022-02-23 07:59:50 +00:00
In addition to passing in `action` and `overrideBrowserWindowOptions` ,
`outlivesOpener` can be passed like: `{ action: 'allow', outlivesOpener: true,
overrideBrowserWindowOptions: { ... } }`. If set to `true` , the newly created
window will not close when the opener window closes. The default value is `false` .
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
2021-04-13 19:36:38 +00:00
const mainWindow = new BrowserWindow()
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 {
2021-06-16 07:16:18 +00:00
action: 'allow',
overrideBrowserWindowOptions: {
frame: false,
fullscreenable: false,
backgroundColor: 'black',
webPreferences: {
preload: 'my-child-window-preload-script.js'
}
2020-11-10 17:06:03 +00:00
}
}
2017-03-19 09:35:12 +00:00
}
2021-06-16 07:16:18 +00:00
return { action: 'deny' }
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
```