---
title: "evaluateJavaScript(_:in:in:completionHandler:)"
framework: webkit
role: symbol
role_heading: Instance Method
path: "webkit/wkwebview/evaluatejavascript(_:in:in:completionhandler:)"
---

# evaluateJavaScript(_:in:in:completionHandler:)

Evaluates a JavaScript string in the context of the specified frame and content world.

## Declaration

```swift
@MainActor @preconcurrency func evaluateJavaScript(_ javaScript: String, in frame: WKFrameInfo? = nil, in contentWorld: WKContentWorld, completionHandler: (@MainActor @Sendable (Result<Any, any Error>) -> Void)? = nil)
```

## Parameters

- `javaScript`: The JavaScript string to evaluate.
- `frame`: The frame in which to evaluate the JavaScript code. Specify nil to target the main frame. If this frame is no longer valid when script evaluation begins, this method returns a doc://com.apple.webkit/documentation/WebKit/WKError/Code/javaScriptInvalidFrameTarget error.
- `contentWorld`: The namespace in which to evaluate the JavaScript code. This parameter doesn’t apply to changes you make to the underlying web content, such as the document’s DOM structure. Those changes remain visible to all scripts, regardless of which content world you specify. For more information about content worlds, see doc://com.apple.webkit/documentation/WebKit/WKContentWorld.
- `completionHandler`: A handler block to execute when script evaluation finishes. The method calls your block whether script evaluation completes successfully or fails. The block has no return value and takes the following parameters:

## Discussion

Discussion The evaluation of your script may change global state in a way that remains visible to subsequent JavaScript code. The changes are restricted to scripts you execute using the same WKContentWorld object. In fact, you can use this method to set up global state in the specified content world, and use that state in subsequent JavaScript code. If you do so, consider using callAsyncJavaScript(_:arguments:in:in:completionHandler:) for more flexible interactions with the JavaScript programming model. After evaluating the script, this method executes the completion handler block with either the result of the script evaluation or an error. The completion handler always runs on the app’s main thread.

## See Also

### Executing JavaScript

- [evaluateJavaScript(_:completionHandler:)](webkit/wkwebview/evaluatejavascript(_:completionhandler:).md)
- [evaluateJavaScript(_:in:contentWorld:)](webkit/wkwebview/evaluatejavascript(_:in:contentworld:).md)
- [callAsyncJavaScript(_:arguments:in:in:completionHandler:)](webkit/wkwebview/callasyncjavascript(_:arguments:in:in:completionhandler:).md)
- [callAsyncJavaScript(_:arguments:in:contentWorld:)](webkit/wkwebview/callasyncjavascript(_:arguments:in:contentworld:).md)
