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 for its intended lifetime. The root Application owns the native view and disposes it during app.exit(), but you need the wrapper object to call methods and listen to events.
Creation Options
Pass aWebviewOptions object to win.createWebview().
For top-level webviews (not child), omit
x, y, width, and height. Doing so lets the webview fill the window and resize with it automatically. Setting explicit bounds fixes the size, which causes a black-border artifact when the window is maximized.Navigation
Load new content or refresh the current page at any time:HeaderData shape:
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,
new-window is dispatched from a separate WebView2 thread and is therefore observational only.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. - Static values and function arguments/results must be JSON-serializable. Cyclic structures,
BigInt, functions as values, andundefinedresults throw aSerializationError. - 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:
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 (Child Webviews)
For child webviews you can reposition or resize the view at runtime without recreating it.Bounds methods are only meaningful for child webviews created with
child: true. On top-level webviews the bounds track the window size automatically.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.