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!
2 Answers 2
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
- Create requirejs_config.js at the below locations
app/code/Companyname/Modulename/view/frontend/requirejs-config.js
- In the above file specify the script.js
var config = {
map: {
"*": {
"somename": "Companyname_Modulename/js/script",
}
},
};
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?MagentoDev– MagentoDev2019年10月17日 14:29:55 +00:00Commented Oct 17, 2019 at 14:29
-
1Yes and if there in your custom extension then you can directly code in it. @SivaMayur– Mayur2019年10月17日 14:31:21 +00:00Commented Oct 17, 2019 at 14:31