How to call JavaScript methods from the Blazor (.razor) pages?
This code will help you to understand how to call JavaScript methods from the Blazor (.razor) pages.
You can call JavaScript methods from the Blazor pages with the help of JavaScript Interop by injecting the dependency IJSRuntime into the razor page.
[script.js]
function buttonClick() {
// this function triggers on button click
}
Then refer the script in the HTML page of the blazor application.
<script src="~/script.js"></script>
[index.razor]
@page "/"
@inject IJSRuntime jsRuntime
<button @onclick="onbuttonclick"> Button </button>
@code {
protected void onbuttonclick (MouseEventArgs args)
{
await jsRuntime.InvokeVoidAsync<object>("buttonClick ");
}
}
I would suggest you check the below link for better understanding:
https://docs.microsoft.com/en-us/aspnet/core/blazor/call-javascript-from-dotnet