Invoking JavaScript from .NET Synchronously
Learn how to call JavaScript from .NET and vice versa.
We'll cover the following
We have only looked at invoking JavaScript functions asynchronously. But we can also invoke JavaScript functions synchronously. We do that by downcasting IJSRuntime
to IJSInProcessRuntime
. IJSInProcessRuntime
represents an instance of a JavaScript runtime to which calls may be dispatched.
IJSInProcessRuntime
allows our .NET code to invoke JS interop calls synchronously. This can be advantageous because these calls have less overhead than their asynchronous counterparts. The methods are similar to the asynchronous methods:
InvokeVoid
Invoke
The following code is the synchronous version of the Document
component. It uses IJSInProcessRuntime
to invoke the JavaScript function synchronously:
Get hands-on with 1400+ tech skills courses.