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!

asked Oct 17, 2019 at 14:04

2 Answers 2

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",
 }
 },
};
answered Oct 17, 2019 at 14:21
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

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.