QuickDevGuide

Home › Blazor

JavaScript Interop — Blazor

Call browser APIs or JS libraries with IJSRuntime, and let JS call .NET with DotNetObjectReference. Task API Call a JS function await JS.InvokeVoidAsync("fn", args) / InvokeAsync<T> Load a module per component await JS.InvokeAsync<IJSObjectReference>("import", "./Map.razor.js") Pass an element @ref="input" → ElementReference JS → .NET instance DotNetObjectReference.Create(this) + [JSInvokable] method JS → static .NET DotNet.invokeMethodAsync("Assembly", "Method") Rules Only call JS in OnAfterRenderAsync (the DOM doesn't exist during prerendering). Use collocated JS modules (Component.razor.js) instead of global scripts. Dispose IJSObjectReference and DotNetObjectReference to avoid leaks.

Key Takeaways