docs

About JavaScript Interop

Expose native functions to the page, call JavaScript from C++, and pass data across the bridge.

On this page

Ultralight offers a safe, high-performance, type-checked JavaScript bridge for C++20 and low-level C. You can use it to expose application data and functions, make calls into JavaScript, and more.

🚧 Preview API

The JavaScript API is a preview. Names and behavior may still change after 2.0.

JavaScript API Quick-Start

Start by defining an "API" (think of it like a JavaScript namespace for all of your application's data/functions), attach it to a View, then load your content per-usual:

C++
// Include "Ultralight/JS.h" to access the optional JS API headers
#include <Ultralight/JS.h>

js::API app("app");

app["version"] = "2.1.0";
app["add"] = [](double a, double b) { return a + b; };

if (app.AttachTo(view.get()))
  view->LoadURL("file:///app.html");

Page script sees a app namespace on window:

JavaScript
app.add(2, 3);  // 5

🚧 Only Local Content Can Access Your Custom API

By default, only local pages (eg, file://) can access native API objects (remote HTTP/HTTPS websites are blocked). To whitelist other URLs, pass origin rules to AttachTo() (see Choosing Which Pages Get the API).

Type-Checked on Both Sides

The JavaScript API is strongly typed. The types you declare are checked on both sidesβ€” once, when you build your C++ code and again, during every runtime JavaScript call.

Rules to Know

πŸ“˜ Build Requirements

The bridge requires C++20β€” on Windows, that means Visual Studio 2022 (MSVC 19.30) or newer. Automatic struct and enum conversion needs Clang, GCC, or MSVC 19.40+ (they don't convert automatically on MSVC 19.30 through 19.39). Every file that uses the bridge must use the same C++ exception setting.

Where to Go

Coming from JSHelpers or raw JavaScriptCore code? The porting tables live in Porting from 1.4 to 2.0. And if your UI needs no page script at all, the DOM API controls pages without any JavaScript.