Invoking JavaScript from .NET Synchronously
Explore how to invoke JavaScript functions synchronously from .NET code by using IJSInProcessRuntime in Blazor. Understand invoking .NET methods from JavaScript with JSInvokable attribute and DotNetObjectReference for seamless interop and efficient UI updates.
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:
InvokeVoidInvoke
The following code is the synchronous version of the Document component. It uses IJSInProcessRuntime to invoke the JavaScript function ...