I'm using magento 2.

I would like to add or call a js file only on the home page , what is the best file to call it?


Just create cms_index_index.xml file in layout folder of your theme/module with following content:

<?xml version="1.0"?>

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

It should work :)

  • it doesn't properly works like this, I did the file as you said , and after I uploaded my js file into /pub/static/frontend/Venustheme/yume/en_GB/js folder , and also written the link as /pub/static/frontend/Venustheme/yume/en_GB/js/myfile.js into the code , but doesn't seems to work.. – alexcr May 11 '16 at 15:45
  • The link should be only js/myfile.js – Bartłomiej Szubert May 11 '16 at 15:53
  • tryed also like this , but still doesn't work.. – alexcr May 11 '16 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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