Skip to main content
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.
Hold a strong JavaScript reference to each 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 a WebviewOptions 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.

Load new content or refresh the current page at any time:
HeaderData shape:
When you provide navigationHandler in the creation options, WebviewJS calls it synchronously before every navigation attempt. Return true to allow navigation or false to block it.
Keep navigationHandler fast and do not return a Promise. The handler runs synchronously on the browser thread. A navigation event is always emitted regardless of whether navigation is allowed or cancelled.

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 calling window.ipc.postMessage(body). Register a handler in Node with:
IpcMessage shape:
Set 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

In the page:

Rules and limitations

  • Only enumerable own data properties of target are exposed. Getters, setters, and inherited properties are ignored.
  • Static values and function arguments/results must be JSON-serializable. Cyclic structures, BigInt, functions as values, and undefined results throw a SerializationError.
  • The namespace name must be a valid JavaScript identifier and can only be exposed once per webview.
See the IPC guide for more detail and the runnable expose example.
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

Call webview.dispose() to release a webview before app.exit():
You can also use the using declaration for automatic cleanup:
Disposal is idempotent — calling it more than once is safe. app.exit() also disposes every webview created under that application. Disposing the parent BrowserWindow also disposes all its webviews.