Milan Lund logo

Hi, I am Milan Lund!Full Stack Web Developer

I specialise in building websites and web applications with and Kentico platforms.

  • Kentico EMS & Xperience

Load a site Javascript file after a Page builder is loaded in Kentico Xperience

In this post, I will provide you with a code that loads a site Javascript file after a Page builder is loaded in Kentico Xperience.


In my sites, I usually put a Javascript file that performs all of the client-side logic right before the closing body tag. Considering Kentico Xperience, I would add it to the main _Layout.cshtml file. 


On the live site, that works well. Regrettably, in the Page builder, the script file gets loaded before the widgets. So, the script is not applied to the markup that is generated by widgets. Moreover, the widgets load after the window.onload event, and therefore there is no Javascript event you can be listening to be able to add the script file dynamically.

I also asked Kentico support, whether there is a Kentico or a .NET way to achieve the goal. I was told there is nothing like it. So, I was looking for a solution on my own.


I ended up loading the Javascript file using an interval checking for the existence of the .ktc-widget-zone element. Once the element is available the script is added to the page. If a page does not contain widgets, the script is added after a certain amount of interval runs.

Add this in your main layout file (i.e. _Layout.cshtml):