JavaScriptKit
Swift framework to interact with JavaScript through WebAssembly.
import JavaScriptKit
Module information
- Declarations
- 297
- Symbols
- 426
Overview
JavaScriptKit provides a seamless way to interact with JavaScript from Swift code when compiled to WebAssembly.
Quick Start
Check out the Quick Start: Hello World tutorial for a step-by-step guide to getting started.
Key Features
Access JavaScript objects and functions
Create closures that can be called from JavaScript
Convert between Swift and JavaScript data types
Use JavaScript promises with Swift’s
async/await
Work with multi-threading
Example
import JavaScriptKit
// Access global JavaScript objects
let document = JSObject.global.document
// Create and manipulate DOM elements
var div = document.createElement("div")
div.innerText = "Hello from Swift!"
_ = document.body.appendChild(div)
// Handle events with Swift closures
var button = document.createElement("button")
button.innerText = "Click me"
button.onclick = .object(JSClosure { _ in
JSObject.global.alert!("Button clicked!")
return .undefined
})
_ = document.body.appendChild(button)
Check out the examples for more detailed usage.
Tutorials
Quick Start: Hello World
This tutorial walks you through creating a simple web application using JavaScriptKit. You’ll learn how to set up a Swift package, add JavaScriptKit as a dependency, write code to manipulate the DOM, and build and run your web application.
Read More
Core Types
enum JSValue
JSValue
represents a value in JavaScript.class JSObject
JSObject
represents an object in JavaScript and supports dynamic member lookup. Any member access likeobject.foo
will dynamically request the JavaScript and Swift runtime bridge library for a member with the specified name in this object.class JSClosure
JSClosure
represents a JavaScript function the body of which is written in Swift. This type can be passed as a callback handler to JavaScript functions.