|
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 friend, The other day I found a bug in StackBuddy, an app I built. Normally I'd open it, file a ticket for myself, and move on. Instead I turned to my AI and just said, "log a dev ticket for that." And it did. A real ticket in my own bug tracker. Title, description, priority, the whole thing. I never opened the actual app. The thing that makes that possible is called MCP, Model Context Protocol. Ignore the name for a second. Out of the box, an AI can only talk. It writes and explains, but...
Hey friend, I don't really write code by hand anymore. I describe what I want, and the AI builds it. For a while, that scared me. If I am not writing the code, does everything I spent years learning still matter? Does my stack matter at all? If you are a developer right now, I would bet you have felt some version of that too. Here is where I landed. It flipped the whole thing for me. The other day I was debugging a slow app, talking it through with the AI. Would WebAssembly help here? Is it...
Hey friend, Lately I told an AI Iād see it in 7 hours, and went to bed. It was done in 2. Across two different projects. Thereās this thing people call the night shift. You give an AI agent a pile of work, start it in a loop, and go to sleep while it keeps working on your real codebase. I tried it a month ago and could not make it work, the model just fell apart. But with the newest models, it works now. And the surprising part wasnāt that it ran all night. Itās that it ran out of work before...