Call and initialize JavaScript

This topic describes different ways to call and initialize JavaScript in Adobe Commerce and Magento Open Source:

We strongly recommend that you use the described approaches and do not add inline JavaScript.

Insert a component in a PHTML template

Depending on your task, you can use declarative or imperative notation to insert a JS component into a PHTML template. Use declarative notation if a component requires initialization and imperative notation in other cases.

Declarative notation

Using the declarative notation to insert a JS component prepares all the configuration on the backend and outputs it to page source using standard tools. Use declarative notation if your JavaScript component requires initialization.

You have two options for specifying declarative notation:

Consider the example of adding a custom carousel JS:

  1. Copy the <carousel_name>.carousel.js file to the app/design/frontend/<package_name>/<theme_name>/web/js/<carousel_name>/ directory.

  2. Add your RequireJS module at app/design/frontend/<package_name>/<theme_name>/web/js/carousel.js.

     define(['jquery','<carousel_name>'], function($)
     {
         return function(config, element)
         {
             $(element).<carousel_name>(config);
         };
     });
    
  3. Add the RequireJS config to the app/design/frontend/<package_name>/<theme_name>/requirejs-config.js file.

    var config = {
        map: {
            '*': {
                    'carousel': 'js/carousel',
                    '<carousel_name>': 'js/<carousel_name>/<carousel_name>.carousel'
                }
            }
    };
    

You now have two options for specifying declarative notation:

Use the data-mage-init attribute

Use the data-mage-init attribute to insert a JS component in a specified HTML element. The following example inserts a JS component in the <nav/> element:

<nav data-mage-init='{"<component_name>": {...}}'></nav>

When the Javascript is inserted into the specified element, the script is called only for this particular element. It is not automatically called for other elements of this type on the page.

How data-mage-init is processed

On DOM ready, the data-mage-init attribute is parsed to extract component names and configuration to be applied to the element. Depending on the type of the inserted JS component, processing is performed as follows:

return {
  '<component_name>': function(config, element) { ... }
};

Where <component_name> is a native JS component, for example: menu, collapsible, tooltip ...

<nav data-mage-init='{"tooltip": {"content": "<?= /* @noEscape */ $content ?>"}}'></nav>

Or a custom JS component, implemented with a component path: Vendor_Module/js/component, or as an alias declared in requirejs-config.js.

<nav data-mage-init='{"Vendor_Module/js/component": {"status":"<?= /* @noEscape */ $block->getStatus(); ?>"}}'></nav>

Read more about locate JS components.

return function(config, element) { ... };
$.fn.<component_name> = function() { ... };
return;

Use the <script type="text/x-magento-init"> tag

To call a JS component on an HTML element without direct access to the element or with no relation to a certain element, use the <script type="text/x-magento-init"> tag and attribute syntax shown in the following example.

<script type="text/x-magento-init">
{
    // components initialized on the element defined by selector
    "<element_selector>": {
        "<js_component1>": ...,
        "<js_component2>": ...
    },
    // components initialized without binding to an element
    "*": {
        "<js_component3>": ...
    }
}
</script>

Where:

The following example provides a working code sample of a widget call using <script>. Here the accordion and navigation widgets are added to the element with the #main-container selector, and the pageCache script is inserted with no binding to any element.

<script type="text/x-magento-init">
{
    "#main-container": {
        "navigation": <?php echo $block->getNavigationConfig(); ?>,
        "accordion": <?php echo $block->getNavigationAccordionConfig(); ?>
    },
    "*": {
        "pageCache": <?php echo $block->getPageCacheConfig(); ?>
    }
}
</script>

Imperative notation

Use imperative notation in the PHTML template to include raw JavaScript code on the pages to execute specified business logic. This method uses the <script> tag without the type="text/x-magento-init" attribute as shown in the following example:

<script>
require([
    'jquery',
    'accordion'  // the alias for "mage/accordion"
], function ($) {
    $(function () { // to ensure that code evaluates on page load
        $('[data-role=example]')  // we expect that page contains the <tag data-role="example">..</tag> markup
            .accordion({ // now we can use "accordion" as jQuery plugin
                header:  '[data-role=header]',
                content: '[data-role=content]',
                trigger: '[data-role=trigger]',
                ajaxUrlElement: "a"
            });
    });
});
</script>
data-variant=success
data-slots=text
For better control when scripts are executed, use a declarative syntax rather than an imperative syntax. When using imperative syntax, the ability to leverage existing JS classes is lost and can block the rendering of the page.

Calling components that require initialization

To call a widget with JS code, use a notation similar to the (accordion widget. It is initialized on the [data-role=example] element as below):

$('[data-role=example]').accordion();

To initialize a widget with options, use notation similar to the following:

$(function () { // to ensure that code evaluates on page load
    $('[data-role=example]')  // we expect that page contains markup <tag data-role="example">..</tag>
        .accordion({ // now we can use "accordion" as jQuery plugin
            header:  '[data-role=header]',
            content: '[data-role=content]',
            trigger: '[data-role=trigger]',
            ajaxUrlElement: 'a'
        });
});

In a similar way, you can initialize any JS component that a returns callback function accepting a config object and element (a DOM node).

For example:

define ([
    'jquery',
    'mage/gallery/gallery'
], function ($, Gallery) {

    $(function () { // to ensure that code evaluates on page load
        $('[data-role=example]')  // we expect that page contains markup <tag data-role="example">..</tag>
            .each(function (index, element) {
                Gallery({
                    options:  {},
                    data: [{
                        img: 'https://c2.staticflickr.com/8/7077/27935031965_facd03b4cb_b_d.jpg'
                    }],
                    fullscreen: {}
                }, element);  // 'element' is single DOM node.
            });
    });
});

Execute data-mage-init and x-magento-init in dynamic content

To trigger .trigger('contentUpdated') on the element when dynamic content is injected, use:

$.ajax({
    url: 'https://www.example.com',
    method: 'POST',
    data: {
        id: '1'
    },
    success: function (data) {
        $('.example-element').html(data)
                             .trigger('contentUpdated')
    }
});