- window.open 函数
- window.open(url[, frameName][, features])
- window.opener.postMessage(message, targetOrigin)
- 使用 Chrome 的 window.open()
window.open 函数
打开一个新窗口并加载 URL。
当调用 window.open 以在网页中创建新窗口时,将为url创建一个新的BrowserWindow 实例,并返回一个代理至 window.open 以让页面对其进行有限的控制。
该代理具有有限的标准功能,与传统网页兼容。要完全控制新窗口,你应该直接创建一个BrowserWindow。
默认情况下, 新创建的 BrowserWindow 将继承父窗口的选项。若要重写继承的选项, 可以在 features 字符串中设置它们。
window.open(url[, frameName][, features])
urlStringframeNameString(可选)featuresString(可选)ReturnsBrowserWindowProxy- 创建一个新窗口,并返回一个BrowserWindowProxy类的实例。
features 字符串遵循标准浏览器的格式,但每个 feature 必须是BrowserWindow 选项中的字段。 These are the features you can set via features string: zoomFactor, nodeIntegration, preload, javascript, contextIsolation, webviewTag.
例如:
window.open('https://github.com', '_blank', 'nodeIntegration=no')
注意:
- 如果在父窗口中禁用了 Node integration, 则在打开的
window中将始终被禁用。 - 如果在父窗口中启用了上下文隔离, 则在打开的
window中将始终被启用。 - 父窗口禁用 Javascript,打开的
window中将被始终禁用 features中给定的非标准特性 (不由 Chromium 或 Electron 处理) 将被传递到additionalFeatures参数中的任何已注册webContent的new-window事件处理程序。
window.opener.postMessage(message, targetOrigin)
messageStringtargetOriginString将消息发送给指定来源的父窗口,如果未指定来源则发送给*,即所有窗口。
使用 Chrome 的 window.open()
如果要使用 Chrome 的内置 window.open(),请在 webPreferences 选项中将 nativeWindowOpen 设置为 true。
原生 window.open () 允许同步打开窗口, 因此可以方便的选择是对话框还是首选项窗口。
该选项也可以设置在<webview>标签上:
<webview webpreferences="nativeWindowOpen=yes"></webview>
BrowserWindow的创建可通过WebContents 的 new-window事件进行定制 。
// main processconst mainWindow = new BrowserWindow({width: 800,height: 600,webPreferences: {nativeWindowOpen: true}})mainWindow.webContents.on('new-window', (event, url, frameName, disposition, options, additionalFeatures) => {if (frameName === 'modal') {// open window as modalevent.preventDefault()Object.assign(options, {modal: true,parent: mainWindow,width: 100,height: 100})event.newGuest = new BrowserWindow(options)}})
// renderer process (mainWindow)let modal = window.open('', 'modal')modal.document.write('<h1>Hello</h1>')
