Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

139
Vistas
Javascript in Razor component

I am trying to load a javascript function on load in a razor component but I am getting an error.

@inject IJSRuntime JsRuntime

@if (listProducts != null)
{
    <table class="table">
        <thead>
            <tr>
                <th>Name</th>
                <th>Home place</th>
                <th>Expiration</th>
                <th>Phone number</th>
                <th>Tea</th>
                <th>Local</th>
                <th>App</th>
                <th>Company /Lease</th>
                <th>Road time</th>
                <th>Notes</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach(var product in this.listProducts)
            {
                <ProductItemComponent 
                    Product="product"
                    OnProductDeleted="OnProductDeleted"></ProductItemComponent>
            }
        </tbody>
    </table>    
}

@code {

    protected override async Task OnInitializedAsync()
    {
        await LoadProducts();
        await JsRuntime.InvokeVoidAsync("exampleJsFunctions.DisableCopy");
    }
}

At the beginning I am injecting IJSRuntime like so @inject IJSRuntime JsRuntime and in the OnInitializedAsync() I am calling my function which is in _Layout.cshtml which looks something like this

<script>
        window.exampleJsFunctions =
            {
                DisableCopy: function (data) {
                    if (document.layers) {
                        document.captureEvents(Event.MOUSEDOWN);
                        document.onmousedown = clickNS4;
                    }
                    else if (document.all && !document.getElementById) {
                        document.onmousedown = clickIE4;
                    }
                  }}
    
    </script>

and this is the error in console enter image description here

Any ideas on what's wrong?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Here is a demo about calling js in blazor:

wwwroot/js/test.js(put the js code into js file of wwwroot):

window.exampleJsFunctions =
            {
                DisableCopy: function (data) {
                    if (document.layers) {
                        document.captureEvents(Event.MOUSEDOWN);
                        document.onmousedown = clickNS4;
                    }
                    else if (document.all && !document.getElementById) {
                        document.onmousedown = clickIE4;
                    }
                  }}

Pages/_Host.cshtml(add reference of test.js in _Host.cshtml):

...
<html lang="en">
<head>
    ...
</head>
<body>
  ...
    <script src="~/js/test.js"></script>
</body>
</html>

Don't call js in OnInitializedAsync().The component is being statically rendered.SO JavaScript interop calls cannot be issued at this time.So you can call it in OnAfterRender.

razor component:

@inject IJSRuntime JsRuntime
@code {
   protected override void OnAfterRender(bool firstRender)
{
    JsRuntime.InvokeVoidAsync("exampleJsFunctions.DisableCopy");
}
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda