I have experience with Magento 1, and there adding a custom javascript file was very straightforward and logical.
But when it comes to Magento 2, I can't figure out how to add a new js file, where I would put little code snippets that affect different places around the page.
For example, I want to modify 'last viewed items' block on the home page, and use jQuery there and then maybe another script that changes some CSS dynamically somewhere (for example).
I find it very comfortable to do it in one single file like I used to do in Magento 1. Also, Magento official docs didn't help me since it didn't provide information clear enough.. I guess it has something to do with requireJS but is there another way? If not then can someone explain it a little clearer than Magento official docs did?
PS. Tried adding my custom.js file to head block trough XML, but then I didn't get access to jQuery.
Thanks!
Best Answer
jQuery is already pre-packaged with Magento2, there is no need to implement it on your own, however, you have to utilize it.
To add JS to your theme you have several possibilities.
By utilizing
default_head_blocks.xml
inapp/design/frontend/Vendor/Theme/Magento_Theme/layout/
:Example Content:
If you inject JS this way, dont forget to wrap your JS into the
requirejs
-conform way:Or: by utilizing
requirejs-config.js
in your themes root folder.Simply add Deps to it, example:
In aboves example I register
main.js
by calling it in the Deps. The path is without the.js
extention, requirejs is handeling this on its own.Your
main.js
should be located atweb/js/
in your theme.The content of
main.js
should be like this: