1

Is it possible to add additional javascript files to just the home page in Magento 2?


The current code works. However the files are placed on every page which is not needed.

Theme/Magento_Theme/layout/default_head_blocks.xml

<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <head>
        <script src="Magento_Catalog::js/sample1.js"/>
        <link src="js/sample.js"/>
    </head> 
</page>

1 Answer 1

1

app/design/frontend/vendorname/themename/Magento_Cms/layout/cms_index_index.xml

   <head>
    <script src="Magento_Catalog::js/sample1.js"/>
    <link src="js/sample.js"/>
   </head>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.