Posted in: Comments

The answer is very simple, but first your template(s) must meet these client resource requirements. The article mention how to register a script on a page level, and in this case it will be injected on ALL templates that meet the requirements, i.e. page type name is XFormPage.

But let’s say you just want to inject a script if a specific block is used on the page. One solution is to use the method in the article and then iterate through all blocks in the page’s content areas and see if the block is used. Probably not the smartest and fastest solution.

A more simple solution is to require the script in the block’s view or in your controller:

var clientResources = ServiceLocator.Current.GetInstance<IRequiredClientResourceList>();

clientResources.RequireScript("/scripts/my-block-script.js", "MyScript", new[] { "MainScripts" }).AtFooter();

If the script doesn't have any dependencies, you can pass Enumerable.Empty<string>() instead of new[] { "MainScripts" }.