2020-11-10 09:06:03 -08: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 09:15:27 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								*  clicking on links or submitting forms adorned with `target=_blank` 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								*  JavaScript calling `window.open()` 
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-04-13 12:36:38 -07: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 09:06:03 -08: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 10:55:17 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								order: parsed options from the `features`  string from `window.open()` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								security-related webPreferences inherited from the parent, and options given by
							 
						 
					
						
							
								
									
										
										
										
											2020-11-18 23:06:32 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								[`webContents.setWindowOpenHandler` ](web-contents.md#contentssetwindowopenhandlerhandler ).
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Note that `webContents.setWindowOpenHandler`  has final say and full privilege
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								because it is invoked in the main process.
							 
						 
					
						
							
								
									
										
										
										
											2015-09-22 22:46:44 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-28 23:21:09 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								### `window.open(url[, frameName][, features])`
 
							 
						 
					
						
							
								
									
										
										
										
											2015-03-04 08:47:04 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-11-16 05:13:18 +01:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								*  `url`  string
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								*  `frameName`  string (optional)
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								*  `features`  string (optional)
							 
						 
					
						
							
								
									
										
										
										
											2015-03-04 08:47:04 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-01-06 09:28:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								Returns [`Window` ](https://developer.mozilla.org/en-US/docs/Web/API/Window ) | null
							 
						 
					
						
							
								
									
										
										
										
											2015-03-04 08:47:04 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								`features`  is a comma-separated key-value list, following the standard format of
							 
						 
					
						
							
								
									
										
										
										
											2023-04-25 15:16:11 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								the browser. Electron will parse [`BrowserWindowConstructorOptions` ](structures/browser-window-options.md ) out of this
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								list where possible, for convenience. For full control and better ergonomics,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								consider using `webContents.setWindowOpenHandler`  to customize the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								BrowserWindow creation.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-04-25 15:16:11 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								A subset of [`WebPreferences` ](structures/web-preferences.md ) can be set directly,
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								unnested, from the features string: `zoomFactor` , `nodeIntegration` , `preload` ,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								`javascript` , `contextIsolation` , and `webviewTag` .
							 
						 
					
						
							
								
									
										
										
										
											2018-09-10 17:49:54 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								For example:
							 
						 
					
						
							
								
									
										
										
										
											2020-11-05 14:12:43 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-09-10 17:49:54 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								window.open('https://github.com', '_blank', 'top=500,left=200,frame=false,nodeIntegration=no')
							 
						 
					
						
							
								
									
										
										
										
											2018-09-10 17:49:54 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2015-09-22 22:46:44 +08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-23 09:08:32 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								**Notes:**
							 
						 
					
						
							
								
									
										
										
										
											2016-10-25 12:35:18 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-10-04 14:41:37 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								*  Node integration will always be disabled in the opened `window`  if it is
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								  disabled on the parent window.
							 
						 
					
						
							
								
									
										
										
										
											2017-04-21 11:00:31 -07: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 14:41:37 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								*  Non-standard features (that are not handled by Chromium or Electron) given in
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  `features`  will be passed to any registered `webContents` 's
							 
						 
					
						
							
								
									
										
										
										
											2021-04-19 15:46:54 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  `did-create-window`  event handler in the `options`  argument.
							 
						 
					
						
							
								
									
										
										
										
											2023-07-24 06:33:41 -04: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 15:16:11 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								*  When opening `about:blank` , the child window's [`WebPreferences` ](structures/web-preferences.md ) will be copied
							 
						 
					
						
							
								
									
										
										
										
											2021-12-06 12:54:14 +09: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 08:47:04 -08:00 
										
									 
								 
							 
							
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08: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 12:36:38 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								process. Returning `{ action: 'deny' }`  cancels the window. Returning `{
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								action: 'allow', overrideBrowserWindowOptions: { ... } }` will allow opening
							 
						 
					
						
							
								
									
										
										
										
											2023-04-25 15:16:11 +02:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								the window and setting the [`BrowserWindowConstructorOptions` ](structures/browser-window-options.md ) to be used when
							 
						 
					
						
							
								
									
										
										
										
											2021-04-13 12:36:38 -07: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 11:12:57 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-23 08:59:50 +01: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 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								### Native `Window` example
 
							 
						 
					
						
							
								
									
										
										
										
											2017-03-19 18:35:12 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-11-20 23:50:08 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// main.js
							 
						 
					
						
							
								
									
										
										
										
											2021-04-13 12:36:38 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								const mainWindow = new BrowserWindow()
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08: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 00:16:18 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      action: 'allow',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								      overrideBrowserWindowOptions: {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        frame: false,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        fullscreenable: false,
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        backgroundColor: 'black',
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        webPreferences: {
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								          preload: 'my-child-window-preload-script.js'
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								        }
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								      }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2017-03-19 18:35:12 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  }
							 
						 
					
						
							
								
									
										
										
										
											2021-06-16 00:16:18 -07:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								  return { action: 'deny' }
							 
						 
					
						
							
								
									
										
										
										
											2017-03-19 20:09:52 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								})
							 
						 
					
						
							
								
									
										
										
										
											2017-03-19 18:35:12 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```
							 
						 
					
						
							
								
									
										
										
										
											2017-03-19 20:30:07 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-11-20 23:50:08 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```js
							 
						 
					
						
							
								
									
										
										
										
											2017-03-19 20:30:07 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								// renderer process (mainWindow)
							 
						 
					
						
							
								
									
										
										
										
											2020-11-10 09:06:03 -08:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								const childWindow = window.open('', 'modal')
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							 
							
							
								childWindow.document.write('< h1 > Hello< / h1 > ')
							 
						 
					
						
							
								
									
										
										
										
											2017-03-19 20:30:07 +09:00 
										
									 
								 
							 
							
								
									
										 
									 
								
							 
							
								 
							 
							
							
								```