I'd like to add a some JavaScript only to the product listings page, is this possible with RequireJS and without touching a phtml file? How?
I looked through the documentation and they suggest only three available methods:
- Declarative notation using the
data-mage-init
. - Declarative notation with the
<script type="text/x-magento-init" />
tag. - Imperative notation with the
<script>
tag.
Each of these methods all require adding/editing a phtml file.
Also, the documentation seems to contradict itself.
We strongly recommend that you use the described approaches and do not add inline JavaScript.
This does not makes sense as the 3rd method they suggest is inline JavaScript!?
These methods all seem a little convoluted and over engineered, I starting to think I should only add one JS file with RequireJS for my entire theme, and have it load on every page, even if it's only being used on a few. Is this good practice? I just want to add some jQuery to a few pages.