{% add_javascript %}

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

Syntax

Syntax: {% add_javascript input [async] [defer] [head|in_place|body] %}

Where 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 "//".

Input may contain one or more filters (eg: {% add_javascript "/path/to/script.min.js" | prepend: cdn_domain async defer head %}).

 

The behavior of this tag depends largely on the arguments supplied. The available arguments are:

async
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.
defer
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.
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. If the same script is added to both the Head AND Body using the add_javascript tag, it will only be included in the head.
If the same script is added synchronously and asynchronously, the script will be loaded synchronously.
If the same script is loaded with deferred processing and immediate processing, it will be deferred.
Options:
  • head (default) requires "{{ header_tags }}" to be present in the document head.
  • 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 requires "{{ body_tags }}" to be present at the end of the document body.

Developer Overview

Liquid Markup