Skip to main content
Logo
There is a newer version of Contensis. Click to find out more and download the latest version.

Add JavaScript to a template

You can add JavaScript files to a template in much the same way as adding in a stylesheet with custom code.

  1. Edit the template you want to add the JavaScript file to.
  2. Click the Custom Code tab and ensure you are adding code into the PagePreload event.
  3. Add the following code to the input area, then Save the template.

CurrentContext.Page.Scripts.Add("/SiteElements/js/scripts.js")

This call to the Web API will add your JavaScript files into the page just before the closing body tag to keep inline with best practices for loading script files. If more than one call to include the same file is registered, the file will only be included once.

Registering jQuery

If you are using jQuery in your web pages you will need to register it's use within the Custom Code tab of your templates. The method used below will include the version of jQuery that Contensis uses. If you need to use a newer version of jQuery you will need to add the file in your SiteElements folder and add it into your template using the method above. Using a newer version may affect some of our controls, this can be alleviated by familiarising yourself with jQuery NoConflict .

If you're happy to use the same jQuery version as Contensis, add the following code to your template and Save it.

CurrentContext.Page.Scripts.RegisterJQuery

Finer control over JavaScript files

See below for a reference on other ways to control the way or where you JavaScript files are loaded.