Magento – Layout Update XML doesn’t properly work for the home page

magento2xml

I'm using magento 2 and I'm trying to add this code into my Layout Update XML.

<reference name="head">
  <action method="addItem">
    <type>skin_js</type><script>jsfilename.js</script>
  </action>
</reference>

But for some reason, my js doesn't get loaded on the home page.

what's wrong with my code ?

Best Answer

I would not advise adding the JS via the XML anymore, Magento 2 best practices are to use Require JS.

Please see the official docs here and here - or my answer to a related question here

Require JS can be added via the template files with data-mage-init attributes (on the element to trigger the script) or a script tag (define the element to trigger the script).

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

<!-- Or with the script tag -->


<script type="text/x-magento-init">
    {
        "#test": {
            "Magento_Cms/js/your-script-name-here": {
            }
        }
    }
</script>