Skip to content

Kirby 3.6.6

js()

Creates a script tag to load a javascript file

js(string|array $url, string|array $options = null): string|null

Parameters

Name Type Default
$url * string|array
$options string|array null

Return type

string|null

Example

Creating a single script tag

<?= js('assets/js/site.js') ?>

Creating multiple script tags

<?= js([
  'assets/js/jquery.js',
  'assets/js/jquery.ui.js',
  'assets/js/site.js',
]) ?>

Autoloading template specific script files

<?= js('@auto') ?>

Template specific JS files must be located in /assets/js/templates and named like the template.

Template JS file
/site/templates/project.php /assets/js/templates/project.js
/site/templates/home.php /assets/js/templates/home.js
/site/templates/blog.php /assets/js/templates/blog.js

Async

If you want the JS files to be loaded asynchronously (if supported by the browser), you can set a second parameter to true:

<?= js('assets/js/site.js', true) ?>

This also works with an array of paths:

<?= js([
  'assets/js/jquery.js',
  'assets/js/jquery.ui.js',
  'assets/js/site.js',
], true) ?>

Other attributes

You can also pass an array of completely custom attributes like the defer attribute:

<?= js('assets/js/site.js', ['async' => true, 'defer' => true, 'data-something' => 'my-value']) ?>