Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

245
Views
JSRuntime not playing sound when initializing Blazor web app

So I have this simple page:

@page "/"
@inject IJSRuntime _jsRuntime

<button @onclick="MakeSound">Make sound</button>
<button @onclick="ShowPopUp">Show pop-up</button>

@code
{
    protected override void OnInitialized()
    {
        _jsRuntime.InvokeVoidAsync("makeSound");
        _jsRuntime.InvokeVoidAsync("showPopUp");
    }

    void MakeSound()
    {
        _jsRuntime.InvokeVoidAsync("makeSound");
    }

    void ShowPopUp()
    {
        _jsRuntime.InvokeVoidAsync("showPopUp");
    }
}

in which I have 2 buttons: one for playing a sound and one for showing an alert. They work fine but when I try to run these actions at initialization, only the alert message shows up and there's no sound. Why?

By the way, this is the JS, though I have a feeling that the problem is not here:

function makeSound() {
    var audio = new Audio("sounds/ding.wav");
    audio.play();
}

function showPopUp() {
    alert("Hello!");
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Calling JavaScript function in OnInitialized do not work, because DOM is not rendered yet, OnAftrRender is the right event to call JavaScript functions. So you can try like this

protected override void OnAfterRender(bool firstRender)
    {
       if(firstRender)
       { 
           _jsRuntime.InvokeVoidAsync("makeSound");
           _jsRuntime.InvokeVoidAsync("showPopUp");
       }
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!