UIWebView
Typehs.ui.webview
A web browser element for embedding in hs.ui.window layouts
Available on macOS 26.0 or later, hs.ui.webview() creates a web browser element backed
by a SwiftUI WebView and WebPage. Embed it in any hs.ui.window using
.webview(element) — it fills the available space and can sit alongside other elements in
stacks.
Create the element first, configure it, then embed it into a window:
const wv = hs.ui.webview()
.toolbar(["back", "forward", "reload", "url"])
.loadURL("https://apple.com")
hs.ui.window({x: 100, y: 100, w: 1024, h: 768})
.titled(true)
.closable(true)
.allowResize(true)
.level("normal")
.webview(wv)
.show()
Because wv is a regular JavaScript object you can keep a reference and call navigation
methods on it at any time after the window is shown:
wv.loadURL("https://google.com")
wv.goBack()
Custom Toolbar Example
const wv = hs.ui.webview()
.toolbar([
"back", "forward", "reload", "url",
{title: "Home", systemImage: "house", callback: () => wv.loadURL("https://apple.com")},
{title: "Reload HS", callback: () => hs.reload()}
])
.loadURL("https://apple.com")
hs.ui.window({x: 100, y: 100, w: 1024, h: 768})
.webview(wv)
.show()
Full Example with Callbacks
const wv = hs.ui.webview()
.toolbar(["back", "forward", "reload", "url"])
.inspectable(true)
.onNavigate((url) => console.log("Navigated to: " + url))
.onTitleChange((title) => console.log("Title: " + title))
.onLoadChange((loading, url, title, progress) => {
if (!loading) console.log("Page ready: " + url)
})
.loadURL("https://apple.com")
hs.ui.window({x: 100, y: 100, w: 1024, h: 768})
.webview(wv)
.show()
Navigation Policy Example
const wv = hs.ui.webview()
.toolbar(["back", "forward", "reload", "url"])
.onNavigationDecision((url) => {
return !url.includes("evil.com")
})
.loadURL("https://apple.com")
hs.ui.window({x: 100, y: 100, w: 1024, h: 768})
.webview(wv)
.show()
JavaScript Evaluation Example
const wv = hs.ui.webview().loadURL("https://apple.com")
hs.ui.window({x: 100, y: 100, w: 1024, h: 768}).webview(wv).show()
// Fire and forget
wv.execJS("document.body.style.backgroundColor = 'lightyellow'")
// With result (note the JS method name is evalJSResult)
wv.evalJSResult("document.title", (result, error) => {
if (error) { console.log("Error: " + error) }
else { console.log("Title: " + result) }
})
Properties
Methods
loadURL(urlString) -> UIWebView
Load a URL in the web view
Declaration
loadURL(urlString) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| urlString | string | The URL to load (e.g. "https://apple.com") |
Returns
UIWebView
Self for chaining
Example
hs.ui.webview().loadURL("https://apple.com")
loadHTML(html) -> UIWebView
Load an HTML string directly into the web view
Declaration
loadHTML(html) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| html | string | The HTML content to display |
Returns
UIWebView
Self for chaining
Example
hs.ui.webview().loadHTML("<html><body><h1>Hello from Hammerspoon!</h1></body></html>")
goBack() -> UIWebView
Navigate back in the browser history
Declaration
goBack() -> UIWebView
Returns
UIWebView
Self for chaining
Example
if (wv.canGoBack) wv.goBack()
goForward() -> UIWebView
Navigate forward in the browser history
Declaration
goForward() -> UIWebView
Returns
UIWebView
Self for chaining
Example
if (wv.canGoForward) wv.goForward()
reload() -> UIWebView
Reload the current page
Declaration
reload() -> UIWebView
Returns
UIWebView
Self for chaining
Example
wv.reload()
stopLoading() -> UIWebView
Stop loading the current page
Declaration
stopLoading() -> UIWebView
Returns
UIWebView
Self for chaining
Example
wv.stopLoading()
userAgent(ua) -> UIWebView
Set a custom User-Agent string for HTTP requests
Declaration
userAgent(ua) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| ua | string | The User-Agent string |
Returns
UIWebView
Self for chaining
Example
hs.ui.webview().userAgent("MyApp/1.0 AppleWebKit")
inspectable(value) -> UIWebView
Enable or disable the Safari Web Inspector for this web view
When enabled, the web view appears in Safari → Develop menu.
Declaration
inspectable(value) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| value | boolean | Pass `true` to enable the Web Inspector |
Returns
UIWebView
Self for chaining
Example
hs.ui.webview().inspectable(true)
toolbar(items) -> UIWebView
Configure the toolbar with a list of standard and custom items
The toolbar renders above the web view. Each element of the array is either a string
naming a standard control or a dictionary describing a custom button.
An empty array (or omitting this call) hides the toolbar.
Standard string items: `"back"`, `"forward"`, `"reload"`, `"url"`, `"spacer"`.
Declaration
toolbar(items) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| items | JSValue | Toolbar items in display order |
Returns
UIWebView
Self for chaining
Notes
The toolbar will not be shown if the web view is in a borderless window
Example
hs.ui.webview()
.toolbar(["back", "forward", "reload", "url",
{title: "Home", systemImage: "house", callback: () => wv.loadURL("https://apple.com")}])
backForwardGestures(enabled) -> UIWebView
Enable or disable the macOS back/forward trackpad swipe gestures
Gestures are enabled by default. Pass `false` to disable them.
Declaration
backForwardGestures(enabled) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | boolean | Pass `false` to disable back/forward swipe gestures |
Returns
UIWebView
Self for chaining
Example
wv.backForwardGestures(false)
magnificationGestures(enabled) -> UIWebView
Enable or disable the trackpad pinch-to-zoom magnification gesture
The gesture is enabled by default. Pass `false` to disable it.
Declaration
magnificationGestures(enabled) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | boolean | Pass `false` to disable pinch-to-zoom |
Returns
UIWebView
Self for chaining
Example
wv.magnificationGestures(false)
linkPreviews(enabled) -> UIWebView
Enable or disable link preview popovers shown on force-click
Link previews are enabled by default. Pass `false` to disable them.
Declaration
linkPreviews(enabled) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| enabled | boolean | Pass `false` to disable link previews |
Returns
UIWebView
Self for chaining
Example
wv.linkPreviews(false)
contentBackground(visible) -> UIWebView
Control whether the web page background is visible
Pass `false` to make the web view background transparent. Enabled (visible) by default.
Declaration
contentBackground(visible) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| visible | boolean | Pass `false` to hide the web content background |
Returns
UIWebView
Self for chaining
Example
wv.contentBackground(false)
onLoadChange(callback) -> UIWebView
Register a callback that fires when loading state or progress changes
Called whenever `isLoading`, `url`, `title`, or `estimatedProgress` changes.
Declaration
onLoadChange(callback) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | Called with current loading state |
Returns
UIWebView
Self for chaining
Example
wv.onLoadChange((loading, url, title, progress) => {
if (!loading) console.log("Finished loading: " + url)
})
onTitleChange(callback) -> UIWebView
Register a callback that fires when the page title changes
Declaration
onTitleChange(callback) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | Called with the new title |
Returns
UIWebView
Self for chaining
Example
wv.onTitleChange((title) => console.log("New title: " + title))
execJS(script) -> UIWebView
Execute JavaScript in the web page without capturing the result
Declaration
execJS(script) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| script | string | The JavaScript code to execute |
Returns
UIWebView
Self for chaining
Example
wv.execJS("document.body.style.backgroundColor = 'lightyellow'")
evalJSResult(script, callback) -> UIWebView
Execute JavaScript in the web page and deliver the result to a callback
The JavaScript method name is `evalJSResult` — it derives from the internal
Objective-C selector `evalJS:result:`.
Declaration
evalJSResult(script, callback) -> UIWebView
Parameters
| Name | Type | Description |
|---|---|---|
| script | string | The JavaScript expression to evaluate |
| callback | function | Called with the result or an error message |
Returns
UIWebView
Self for chaining
Example
wv.evalJSResult("document.title", (result, error) => {
if (error) { console.log("Error: " + error) }
else { console.log("Title: " + result) }
})