title: Blazor - Call JavaScript from C# class
body: <p>Here is what I want to achieve:</p>
  <p>I have a (normal) class in a Blazor WASM project. I want to invoke a JavaScript function from my class.
  If I want to do this from a Razor component it is working fine, I inject IJSruntime and invokevoidasync to my JavaScript.</p>
  <p>But it goes wrong when I try to do it from a class.
  First I tried to inject it like this:</p>
  <pre><code>[Inject]
  IJSRuntime JSRuntime { get; set; }
  </code></pre>
  <p>But ended up with error message: Value cannot be null. (Parameter 'jsRuntime')
  I learned from <a href="https://stackoverflow.com/questions/59628714/blazor-client-wasm-jsinvoke-value-cannot-be-null-error">this</a> post that I have to &quot;Inject it in the traditonal way&quot;, so I did this:</p>
  <pre><code>public class InvokeJavaScript
  {
      private readonly IJSRuntime jSRuntime;
  
      public InvokeJavaScript(IJSRuntime jSRuntime)
      {
          this.jSRuntime = jSRuntime;
      }
  
      public async void InvokeMyJs()
      {
          await jSRuntime.InvokeVoidAsync(&quot;giveMeAMessage&quot;);
      }
  }
  </code></pre>
  <p>But from there on I am stuck, I know that this must be some key .NET knowledge but I am missing a piece here.. I want to call the &quot;InvokeMyJs&quot; methode like:</p>
  <pre><code>InvokeJavaScript ij = new InvokeJavaScript();
  ij.InvokeMyJs();
  </code></pre>
  <p>But know I am facing an error: There is no argument given that corresponds to the required formal parameter 'jSRuntime' of 'InvokeJavaScript.InvokeJavaScript(IJSRuntime)'</p>
  <p>That I get the error makes sense to me but I dont know how to fix it, what parameter must I send to InvokeJavaScript(IJSRuntime jSRuntime) and how do I do it correctly? Can anyone give an example?</p>