{% add_javascript %}

Use this tag to add a script to the current page.

There are two ways to use this method. The first (and more common) is to include a linked javascipt (<script src="https://..."></script>) and the second is to include an inline script (<script>...</script>)

Linked Javascript Syntax

{% add_javascript
input
 
Input could evaluate to a javascript object, the guid of a script, the name of a script, or a URL. In order to add a script using a URL, you must use a fully-qualified URL, incuding the leading "http://", "https://", or "//".
position:[head|in_place|body]
 
Determines where the script will be placed in the document.
Any scripts added to the Head or Body will only be added once, although additional attributes (if any) will be combined from each time it is added. If the same script is added to both the Head AND Body using the add_javascript tag, it will be included in the head.
head is the default and places the script in the document's <head> element.
in_place outputs the script where the add_javascript tag is located in the markup. A script may be added "In Place" as many times as desired.
body places the script directly before the document's &lgt;/body> closing tag.
async:true|false
 
If async is specified, the script will be loaded asynchronously on browsers that support the async attribute. The default behavior is to load scripts synchronously. If the same tag is added multiple times with both true and false specified, the script will be loaded synchronously.
defer:true|false
 
If defer is specified, script processing will be deferred until after the page has loaded on browsers that support the defer attribute. The default behavior is to process the script immediately. If the same tag is added multiple times with both true and false specified, script processing will be deferred.
attribute1:"add or edit other attributes"
 
This method will accept any number of parameters and evaluate them before including them as attributes of the script tag. You may also include reference variables, which will be dereferenced into their attribute names.
%}

Syntax for Inline Script

{% add_javascript inline
unique_name
 
If included, only one inline script will be included for each unique_name. This makes it safe to add an inline script in one or more sub-templates which may be included multiple times in a page.
=
position:'[head|in_place|body]'
 
Determines where the script will be placed in the document.
head is the default and places the script in the document's <head> element.
in_place outputs the script where the add_javascript tag is located in the markup. A script may be added "In Place" as many times as desired.
body places the script directly before the document's &lgt;/body> closing tag.
attribute1:"add or edit other attributes"
 
This method will accept any number of parameters and evaluate them before including them as attributes of the script tag. You may also include reference variables, which will be dereferenced into their attribute names.
%}
...code block...
{% end_javascript %}

The script to be included will be evaluated from the code block similar to the {% capture %} method.

Examples

Add Javascript

Copy
{% add_javascript "/jQuery" %}

Add Deferred Async Javascript To Body

Copy
{% add_javascript "/main" defer:true async:true position:'body' %}

Add Anonymous Javascript In Place

Copy
{% add_javascript "https://cdn.thirdpartyservice.com/path/to/script" position:'in_place' crossorigin:'anonymous' %}

Add inline javascript once to body

Copy
{% add_javascript inline xxkr_once = position:'body' %} function doSomething() { alert('I did something!'); } doSomething(); {% end_javascript %}