simple working page. create new blazor server app (net 8)
% dotnet new blazor
add this page:
@page "/alert"
@rendermode InteractiveServer
@inject IJSRuntime JsRuntime
<script>window.showAlert = m => alert(m);</script>
<PageTitle>Alert</PageTitle>
<h1>Alert</h1>
<button class="btn btn-primary" @onclick="ShowAlert">Click me</button>
@code {
private async Task ShowAlert()
{
await JsRuntime.InvokeVoidAsync("showAlert", "Hello from JS interop!");
}
}
note:
await JsRuntime.InvokeVoidAsync("alert", "Hello from JS interop!");
will also work and not require a javascript function be defined