0

I have created a custom module for Megamenu. I have js file which needs to be added to this module.

Some of my queries are:

  • Can I add the js file directly on my module?. If yes, how to do it?
  • Do I need to add it on theme? If that's right, how to do it?

Any help will be appreciated!

0

You can add the js file directly on your module. Follow the steps below

1.Add your js file in the path

app/code/Companyname/Modulename/view/frontend/web/js/script.js

  1. Create requirejs_config.js at the below locations

app/code/Companyname/Modulename/view/frontend/requirejs-config.js

  1. In the above file specify the script.js
var config = {
    map: {
        "*": {
            "somename": "Companyname_Modulename/js/script",
        }
    },

};
0

Step 1 : Move your file in VENDOR/EXTENSION/view/frontend/web/js/some_name.js

Step 2 : Add your file in layout like module_index_index.xml

<head>
    <script src="VENDOR_EXTENSION::js/some_name.js"/>
</head>

Step 3 : Execute Command php bin/magento setup:static-content:deploy

Then give permission if required.

Now, after refreshing the page and check in the view source you will get this file.

Thanks

  • We have to create manually the file module_index_index.xml, am i right? – Siva Oct 17 '19 at 14:29
  • 1
    Yes and if there in your custom extension then you can directly code in it. @Siva – Mayur Oct 17 '19 at 14:31

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.