Pros and Cons of Using JavaScript Interop in Blazor
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (180).NET Core  (28).NET MAUI  (224)Angular  (113)ASP.NET  (49)ASP.NET Core  (81)ASP.NET MVC  (87)Azure  (41)Black Friday Deal  (1)Blazor  (234)BoldSign  (14)DocIO  (24)Essential JS 2  (110)Essential Studio  (199)File Formats  (72)Flutter  (135)JavaScript  (226)Microsoft  (121)PDF  (83)Python  (1)React  (105)Streamlit  (1)Succinctly series  (131)Syncfusion  (960)TypeScript  (33)Uno Platform  (3)UWP  (3)Vue  (46)Webinar  (53)Windows Forms  (59)WinUI  (72)WPF  (162)Xamarin  (159)XlsIO  (38)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (10)Business intelligence  (55)Button  (4)C#  (163)Chart  (145)Chart of the week  (57)Cloud  (15)Company  (440)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (73)Development  (676)Doc  (7)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (7)Essential Tools  (13)Excel  (43)Extensions  (23)File Manager  (7)Gantt  (20)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (519)Mobile MVC  (9)OLAP server  (2)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (45)Performance  (13)PHP  (2)PivotGrid  (5)Predictive Analytics  (6)Report Server  (3)Reporting  (8)Reporting / Back Office  (9)Rich Text Editor  (12)Road Map  (12)Scheduler  (54)Security  (5)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (32)Solution Services  (4)Spreadsheet  (11)SQL  (15)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (113)UI  (400)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (19)Web  (624)What's new  (337)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Pros and Cons of Using JavaScript Interop in Blazor

Pros and Cons of Using JavaScript Interop in Blazor

TLDR: Does JavaScript interop improve Blazor or complicate it? This blog evaluates the pros and cons. It discusses interop’s benefits like accessing browser APIs and existing JavaScript code integration. However, it also warns of performance issues, security risks, and added complexity. Developers integrating JavaScript into Blazor should weigh these factors.

Blazor is an open-source, single-page web application development framework. Unlike other frameworks, such as Angular, React and Vue, which depend on JavaScript libraries, Blazor allows you to write and run C# code in web browsers via WebAssembly.

The Blazor framework can, however, call JavaScript functions from .NET (C#) methods and vice versa. It handles the DOM (document object model) manipulation and browser API calls through a method called JavaScript interoperability (JS interop).

We can also use TypeScript in Blazor, as TypeScript is the superset of JavaScript. When we compile a Blazor project, the TypeScript file will be converted into a JavaScript file using the MSBuild properties.

In this blog post, we will see how to use JavaScript interop in Blazor, its pros, and its cons.

Invoke JavaScript functions from .NET methods

In a Blazor app, to call JavaScript functions from .NET, we need to inject the IJSRuntime abstraction and call the InvokeAsync method. The InvokeAsync method accepts the function name and number of arguments that the function requires.

ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args);

This method has three arguments: function name, CancellationToken (for notification of whether the operation is canceled or not), and the number of arguments that the function requires.

ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args)

Explore the best and most comprehensive Blazor UI components library in the market.

Invoke .NET methods from JavaScript functions

To call static .NET methods from JavaScript functions in a Blazor app, use the DotNet.invokeMethod or DotNet.invokeMethodAsync method.

Refer to the following code example.

DotNet.invokeMethodAsync('{ASSEMBLY NAME}', '{.NET METHOD ID}', {ARGUMENTS});

The previous method has three arguments:

  • Application assembly name.
  • .NET method name (identifier).
  • The number of arguments (optional) that the function requires. (Note: Each argument should be JSON-serializable).

Pros of JS interop

Let’s see the advantages of using JavaScript interop in your Blazor application.

Injecting a script

Using the JavaScript interop, we can easily inject the required code on-demand anywhere in our Blazor app. You can see the injected JavaScript function after the DOM is loaded in the Blazor app (both WebAssembly and Server apps).

Follow these steps to inject a script file in Blazor:

  1. First, set the autostart attribute as false in the script tag <script>.
  2. Then, inject the required script using the start().then(…) method. The injected function will be called after starting the Blazor app.
  3. Next, create the script tag using the JavaScript function createElement and set the custom script file path in the src attribute.
  4. Finally, append the script element to the head method.

Refer to the following code example.

 <script src="_framework/blazor.{webassembly|server}.js"
   Blazor.start().then(function () {
      var customScript = document.createElement('script');
      customScript.setAttribute('src', 'scripts.js');

JavaScript isolation

Blazor allows JavaScript isolation in standard JavaScript modules. This JavaScript isolation feature provides the following benefits:

  • JavaScript code will load only the specified components. This will save memory and rendering time.
  • Imported JavaScript code does not affect any global namespace.
  • We don’t need the library and component consumers to import the related JavaScript.

Everything a developer needs to know to use Blazor components in the Blazor app is completely documented.

Use any third-party JavaScript framework

One of the major advantages of using JavaScript interop is that we can integrate any of the JavaScript frameworks into the Blazor app and achieve its functionalities.

In the following code, we are going to call the Syncfusion JS 2 JavaScript library animation functions in our Blazor server app.


  <link href="JSinterop.styles.css" rel="stylesheet" />
  @*Syncfusion library CDN script and CSS reference*@
  <link href="" rel="stylesheet">
  <script src="" type="text/javascript"></script>
      window.initializeAnimation = () => {
         //initialize the Animation code.
         var animation = new ej.base.Animation({ duration: 5000 });
         animation.animate('#element1', { name: 'FadeOut' });
         animation.animate('#element2', { name: 'ZoomOut' });


@page "/"
@inject IJSRuntime JsRuntime;
  #element1, #element2 {
    background: #333333;
    border: 1px solid #cecece;
    box-sizing: border-box;
    float: left;
    height: 100px;
    width: 100px;
<h1>Hello, world!</h1>
  <div id="element1"></div>
  <div id="element2"></div>
  bool firstRender = true;
  protected override async Task OnAfterRenderAsync(bool firstRender)
    if (firstRender)
      // call the JS function.
      await JsRuntime.InvokeVoidAsync(

Cached JavaScript files

In a web platform, while caching static resources (the JavaScript file on the client-side browser), a static file will be loaded from the cache by default. This cache file will reduce the number of requests to the server, so it enhances the loading speed of the pages.

You can further improve the performance of the browser cache using the catche-control with the value of no-catch or by setting the max-age value.

Asynchronous JavaScript method calls

By default, the JavaScript interop calls asynchronous methods, so it is compatible with both Blazor server and WebAssembly apps.

Note: Blazor server’s JS interop calls should be asynchronous, as they’re sent over a network connection.

See how Syncfusion Blazor components can be transformed into beautiful and efficient applications.

Cons of JavaScript interop

Even though the JavaScript interop in Blazor has so many advantages, there are some limitations to it.

Directly modifying the DOM with JavaScript isn’t recommended

The DOM is used for display purposes alone in browsers. Modifying the DOM using JavaScript code is not suggested, anyway, as JavaScript restricts updating a Blazor element’s tracked changes.

Consider a situation where an element is rendered by Blazor code and modified externally using JavaScript directly or via JavaScript interop. Then, the DOM structure may not match Blazor’s internal representation. This may lead to undefined behaviors.

JavaScript interop call timeouts

The JavaScript interop call may fail due to a network issue and low bandwidth network in a server app. By default, it takes a one-minute timeout for each JavaScript call. Luckily, we have an option to increase the timeout value in the server app. But this may also lead to performance deterioration.

Refer to the following code.


  options => options.JSInteropDefaultCallTimeout = {TIMEOUT});

We can override the global timeout set using the JSInteropDefaultCallTimeout method.


var result = await JS.InvokeAsync<string>("{ID}", {TIMEOUT}, new[] { "Arg1" });

Size limits on JavaScript interop calls

In a Blazor server app, JavaScript interop calls are limited in size while maximizing the incoming SignalR message permitted for hub methods.

If we pass a huge message (more than 32 KB) in a JS call, then it will throw an error.

However, we can increase the size of the SignalR message in the Program.CS file like in the following code.


  .AddHubOptions(options => options.MaximumReceiveMessageSize = 64 * 1024);

Increasing the message size will also increase the risks for the user. Additionally, reading a huge amount of content into memory as strings or byte arrays can also result in poor allocation of memory with the garbage collector. Further, it results in additional performance penalties.

Other performance issues

A Blazor app will experience poor performance when we serialize a huge amount of .NET objects and sent them to the JS interop call. For example, serializing huge .NET objects rapidly in the resize and mouse wheel events.

Calling the JS interop frequently may lead to performance lag. The synchronous calls that don’t perform JSON serialization of arguments or return values, the memory management, and translations between .NET and JavaScript also result in poor performance.

Syncfusion Blazor components can be transformed into stunning and efficient web apps.


Thanks for reading! In this blog, we have seen JavaScript interop concepts used in the Blazor framework along with their pros and cons. With this JS interop, we can easily call JavaScript functions from .NET (C#) methods and vice versa.

Syncfusion’s Blazor component suite offers over 70 UI components that work with both server-side and client-side (WebAssembly) hosting models seamlessly. Use them to build marvelous applications!

If you have questions, you can contact us through our support forumsupport portal, or feedback portal. As always, we are happy to assist you!

Related blogs


Share this post:

Comments (1)

In regards to your comment about serializing large objects and seeing delays with JSRuntime. I noted that if I used the technique of opening a window and passing in it’s image or pdf content rle encoded, the memory was never returned. I think it was something to do with the circuit closing — and a ref staying somewhere keeping the GC from recycling. I reported it to .net team, but just got a genius informing me that, of course i was losing memory because I passed such a lot of memory to it — which I was to demo the leak. lol. I’m sure they’ll fix the issue at some point when someone not quite so lazy notes the issue.

Comments are closed.

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed