Add_javascript
{% add_javascript %}
Add a script asset to the current page via a <script> tag
Related
Add a stylesheet asset to the head of the current page via a <link> tag
Outputs javascript code in an inline script
Outputs an inline stylesheet in a <style> tag.
{% add_javascript inline %}
Outputs javascript code in an inline script
This method creates a new liquid context for storing and manipulating variables.
Related
Add a stylesheet asset to the head of the current page via a <link> tag
Add a script asset to the current page via a <script> tag
Outputs an inline stylesheet in a <style> tag.
Examples
Add JavaScript to the body with deferred or async loading (e.g. with add_javascript options).
{% add_javascript "/main" defer:true async:true position:'body' %}
Add inline anonymous JavaScript in place (e.g. for one-off scripts) with add_javascript inline.
{% add_javascript "https://cdn.thirdpartyservice.com/path/to/script" position:'in_place' crossorigin:'anonymous' %}
Add linked or inline JavaScript with add_javascript; use path, attributes, or inline blocks (once, in place, with nonce).
{% add_javascript "/jQuery.js" %}
Adds the javascript file from the specified path to the page. This is the simplest way to add javascript to a page, and by default adds it in the head of the page.
{% add_javascript "/jQuery" position:'head' async:true defer:true %}
Adds the javascript file to the head of the page with the async and defer attributes.
{% add_javascript "/https://cdn.thirdpartyservice.com/path/to/script" position:'body' crossorigin:'anonymous' integrity:'sha384-...' %}
Adds the third-party javascript before the </body> tag with the additional custom attributes.
{%- add_javascript inline xxkr_once = position:'body' -%}
function doSomething() {
alert('I did something!');
}
doSomething();
{%- end_javascript -%}
Adds the inline javascript to the body of the page. Because the script is named (xxkr_once), the javascript will only be added once and can be used in one or more partial templates which may be included multiple times in a page. Each script to be included should have a unique name.
{%- add_javascript inline xxkr_once -%}
doSomething();
{%- end_javascript -%}{%- add_javascript inline xxkr_once -%}
doSomethingElse();
{%- end_javascript -%}
Because both scripts use the same name (xxkr_once), only the first script will be added to the page and the second script will be ignored. This may sometimes be the desired behavior, but to include both scripts either remove the name or ensure that both scripts have unique names.
{%- for item in (1..3) -%}
{%- id itemid = prefix:'item_' -%}
<div id="{{itemid}}">{{item}}</div>
{%- add_javascript inline = position:'in_place' -%}doSomething('{{itemid}}');
{%- end_javascript %}
{% endfor -%}
Adds the inline javascript immediately to the template, which will result in a separate script tag for each item.
{%- var nonce = '...' -%}
{%- add_javascript inline = position:'head' nonce:nonce -%}
doSomething();
{%- end_javascript -%}
Adds the inline javascript to the head of the page with a nonce attribute. The nonce attribute is used to prevent cross-site scripting (XSS) attacks by ensuring that the script is only executed if the nonce is valid. The logic for determining the nonce is left to the developer.