Hey Reader, Today, I show you how combining Blazor and JavaScript can give you the best of both worlds. Letās dive into how you can easily mix the power of these two technologies in your projects! Watch on YouTube šŗCheck out the full tutorial on YouTube to see everything in action š A Simple Example: Console Logging in BlazorImagine youāre working on a Blazor Server application, and you want to log the current count (of the Counter page) to the console. Your first thought might be to use
This works, but youāll only see the output in the terminal, not the browser console. But what if you want to log it directly in the browser console? This is where JavaScript comes in handy. Injecting JavaScript into BlazorTo use JavaScript, you can inject the
When you run this, youāll see the log directly in your browserās console. This is a basic example, but you can use this approach to call any JavaScript function from Blazor. Using Custom JavaScript FilesBut what if you want to use your own JavaScript functions? Just create a new JavaScript file in your projectās
Now, reference this file in your Finally, call this custom function from Blazor:
When you click the button, youāll see your custom log message in the console. Logging Complex ObjectsWant to log an entire object instead of just a text or value? You can easily do this with JavaScript too. For example, letās say you have a
You can log this object directly:
JavaScript will display the object in the console, showing all its properties. Calling Blazor from JavaScriptSometimes, you might want to call a Blazor function from JavaScript. Although itās less common, itās possible! Hereās a quick example: Create a static Blazor method and mark it with the
In your JavaScript, call this method:
When this JavaScript function runs, it calls the Blazor method and increments the counter. Wrapping UpCombining Blazor with JavaScript opens up a world of possibilities for your web development projects. Whether youāre logging data, handling complex objects, or calling Blazor methods from JavaScript, these techniques can make your applications more powerful and flexible. What's your experience with Blazor and JavaScript? Any real-world examples where you need a specific JavaScript library? Happy coding and have an awesome weekend! Take care, Patrick PS: Need help understanding .NET & Blazor? There are two ways I can help you with:
PPS: Would you like to sponsor this newsletter? Click here. š |
Become a .NET & Blazor expert with weekly tutorials featuring best practices and the latest improvements, right in your inbox.
Hey Reader, Last week, I tested GPT-5 Agent Mode on .NET and Blazor apps, and the results were surprising. This week, I went a step further. I used GitHub Copilot inside Visual Studio and asked it to create real .NET and Blazor projects for me. Hereās what happened: Copilot generated a working calculator app in C#. It built a Web API that responded with āHello GPT-5 Agents.ā It even created a Blazor app with extra features like a reset button, a plus five counter, and a weather API. Were...
Hey Reader, I just published a new video where I put GPT-5ās Agent Mode to the test. š Instead of asking it for something simple, I went all in: A .NET 9 console app calculator A Web API with endpoints and docs A Blazor Server app with extra counter buttons And even a Blazor app that calls a public weather API š„ Watch it now: In the video, I didnāt just type āmake me an appā and hope for the best. I gave GPT-5 very specific prompts, like: āPlease create a .NET 9 console application that works...
Hey Reader, Iām back! š Iāve been on parental leave for the past 2 months, enjoying time with the family. Now Iām diving back into videos and hereās the first one! š„ Watch it now: Hereās the TL;DR if you canāt click right now: AI can already write code, fix bugs, and explain complex concepts. So⦠are we doomed? No. AI isnāt replacing developers anytime soon. But: Developers who use AI will replace those who donāt. The best devs are using GPT to: Skip repetitive work Prototype faster Learn...