Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run js code in blazor without having to call a function

Tags:

blazor

I couldn't find anything that would allow me to run js code in blazor without having to call a function from a js file and I'm wondering if it is possible, since it would be much easier to just write some js in my blazor component.

Something like this:

@inject IJSRuntime JSRuntime

@code {
    protected override void OnInitialized()
    {
        JSRuntime.InvokeVoidAsync(
        { 
            // Write JS in here, like this:
            console.log("i possible hope this is");
        });

        StateHasChanged();
    }
}

Is something like the example above a possibility?

like image 944
spindi598 Avatar asked Oct 24 '25 05:10

spindi598


1 Answers

you have to call a function but can work around as eval is a function takes JS code used it to inject JS from library projects https://blog.ppedv.de/post/Razor-Classlibrary-JavaScript-einbinden

like image 85
HannesPreishuber Avatar answered Oct 27 '25 04:10

HannesPreishuber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!