Link a javascript file to a page not a template. Explained in simple terms.

Ismael Feliciano asked on September 17, 2020 23:33

I was wondering if anyone could help and explain to me how to link a javascript file or a script to a page or several pages not to a template. The documentation isn't very clear to me on how to accomplish this in Kentico 10. I would love to be able to use a header widget on the page which includes the script if possible. Thanks in advance!

Recent Answers


David te Kloese answered on September 18, 2020 11:09

When not embedding them directly from the Masterpage you usually add them to a template using a Web Part. The 'Head HTML Code' Web Part is suited for this.

If you want to make an exception it's usually best to just create a different template for the other pages. But in case it's depending on some other aspect you could use the enabled setting of the 'Head HTML Code' Web Part.

  • Add the 'Head HTML Code' Web Part to the Template and add your JS reference
  • based on your requirement let the Web Part only be Enabled when a certain condition is met

Click the Arrow icon to open the popup where you can use a macro:

Image Text

e.g. When current document has a special boolean set to true or a certain field has a value.

Something like:

{% (bool)CurrentDocument.LoadScript %}

or

{% CurrentDocument.SpecialCategory == 'SpecialPage' %}

Can also be something completely different, but you didn't share the requirement.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on September 19, 2020 14:58

What we have done is create a widgets based on the Head HTML Code and JavaScript web parts. Then update the templates to have a widget areas where we can add those widgets to. Widgets are page specific whereas wep parts are not

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.