Webview controls the embedded browser view attached to a BrowserWindow. You create a Webview by calling win.createWebview(options) on an existing window. Each window can host one or more webviews, and a webview can display any URL, inline HTML, or content served by a custom protocol.
Webview wrapper available for as long as you need to call its
methods or listen to its events. The application and owning window retain the
native view and dispose it during app.exit().
Calling
new Webview() directly is not supported. Create webviews through a
BrowserWindow.Creation Options
Pass aWebviewOptions object to win.createWebview().
For a normal top-level webview on Windows and Linux, omit
x, y, width, and height so the view fills the window and follows its size. A child: true view or any explicit bound opts into bounds mode; omitted positions default to 0, and omitted dimensions default to 800 × 600. On macOS, the native implementation builds every webview as a child view and derives initial bounds from the host window when you omit them.The native constructor receives the initial width and height as a physical size. Runtime setBounds() and the bounds getters use the backend’s logical geometry, so use setBounds() when you need logical child-view positioning or resizing.Navigation
Load new content or refresh the current page at any time:HeaderData shape:
autoNormalizeLoadUrl: false to disable that behavior, or set useHttpsScheme: true to use the HTTPS form of the workaround.
Navigation handler
When you providenavigationHandler in the creation options, WebviewJS calls it synchronously before every navigation attempt. Return true to allow navigation or false to block it.
Script Execution
Run JavaScript in the page context:Webview Events
Webview extends Node.js EventEmitter. Use .on(), .once(), .off(), .addListener(), .removeListener(), and .removeAllListeners() to manage listeners.
Event reference
Download events are observational — you cannot cancel a download from these events.
On Windows, the
new-window listener is observational; navigationHandler is
still evaluated separately when you provide one.Usage example
IPC
The page sends a message to Node by callingwindow.ipc.postMessage(body). Register a handler in Node with:
IpcMessage shape:
ipcName: 'bindings' in WebviewOptions to also expose window.bindings as an alias for window.ipc. The default window.ipc global is always available.
See the IPC Messaging guide for a complete walkthrough.
expose()
expose() is a higher-level IPC helper that makes Node.js values and functions available as a named global in the page. Every exposed function becomes a Promise-returning stub in the browser, even if the Node.js implementation is synchronous.
Example
Rules and limitations
- Only enumerable own data properties of
targetare exposed. Getters, setters, and inherited properties are ignored. - Enumerable own data properties whose values are functions become async page functions; other exposed property values are serialized with
JSON.stringify. Cyclic structures,BigInt,Symbol, and anundefinedtop-level value throw aSerializationError.MapandSetfollow normalJSON.stringifybehavior, usually becoming{}; nestedundefinedproperties are omitted. - The namespace
namemust be a valid JavaScript identifier and can only be exposed once per webview.
Cookie Management
Read and write the webview’s cookie store:WebviewCookie shape:
"strict", "lax", or "none" for sameSite. The native mapping treats any other value as "lax".
DevTools
Open, close, or check the browser developer tools:DevTools must be enabled at creation time via
enableDevtools: true in WebviewOptions.Appearance
Set the background color shown before (or behind) page content. Each component is an integer in the range 0–255:Focus
Control which element holds keyboard input:Bounds
For child or explicitly bounded webviews, you can reposition or resize the view at runtime without recreating it.Bounds methods are most useful for child webviews created with
child: true.
A top-level webview created without explicit bounds tracks the host window
size automatically.The application’s window-resize handler currently reapplies the full client
rectangle to every tracked webview. If a child or explicitly bounded view
must keep a sub-rectangle after its host window is resized, call
setBounds()
again from the window’s resize listener.Disposal
Callwebview.dispose() to release a webview before app.exit():
using declaration for automatic cleanup:
app.exit() also disposes every webview created under that application. Disposing the parent BrowserWindow also disposes all its webviews.