🎓 All Courses | 📚 Blazor WASM Syllabus
Stickipedia University
📋 Study this course on TaskLoco

While Blazor WASM runs C# in the browser, you sometimes need to call JavaScript — for browser APIs, third-party JS libraries, or DOM manipulation Blazor cannot do natively.

Calling JavaScript from C#

@inject IJSRuntime JS

// Void call:
await JS.InvokeVoidAsync("console.log", "Hello from C#!");

// Call that returns a value:
string result = await JS.InvokeAsync<string>("getLocalStorageItem", "token");

Defining Custom JS Functions

// wwwroot/app.js
window.showAlert = (message) => alert(message);
window.scrollToTop = () => window.scrollTo(0, 0);

Calling C# from JavaScript

// C# — mark method as invokable:
[JSInvokable]
public static string GetGreeting() => "Hello from .NET!";

// JS:
const result = await DotNet.invokeMethodAsync('MyApp', 'GetGreeting');

JS Module Isolation

Use IJSRuntime.InvokeAsync<IJSObjectReference> to import a JS module and avoid polluting the global window scope.


YouTube • Top 10
Blazor WASM: JavaScript Interop (JSInterop)
Tap to Watch ›
📸
Google Images • Top 10
Blazor WASM: JavaScript Interop (JSInterop)
Tap to View ›

Reference:

Wikipedia: WebAssembly

image for linkhttps://en.wikipedia.org/wiki/WebAssembly

📚 Blazor WASM — Full Course Syllabus
📋 Study this course on TaskLoco

TaskLoco™ — The Sticky Note GOAT