Calling JavaScript from C# code
Now that we have learned why we need JS and the possible ways to reference it, it’s time to get some JS code executed by our Blazor app. We will cover three different scenarios:
- Calling a basic JS method
- Calling a JS method synchronously
- Calling a JS method that returns data
Calling a basic JS method
In the previous section, we created the site.js
file that contains the showAlert
function. In the following exercise, we will call that function from the NavBar component where we will add a new Login button on the top-right side of our app.
For now, this button will show an alert, but later we will use it for authentication purposes:
- In the
Shared
folder, open the NavBar component and add a button with its owndiv
, so by default, the button will show on the right-hand side of the page:<nav class="navbar navbar-expand-lg navbar-light bg-light"> .....