Hi,
using 2.2.2 in developer mode. I've put this inside the <head> tag of default_head_blocks.xml:
<script src="js/query.matchHeight.js" />
and ofc I did put the file inside the web/js folder of my child theme, but the page tells me cannot find the file. I also forced a deploy static content with no luck.
What am I missing?
Thanks a lot
Hi .
Magento 2 use RequireJs . You should not add js directly to xml file .
Hello januaryae_ryae
The best way to add JS is with requirejs.
app/design/frontend/{Vendor}/{theme}/requirejs-config.js
var config = { map: { '*': { myscript: 'js/myfile' } } };
app/design/frontend/{Vendor}/{theme}/web/js/myfile.js
define(['jquery'], function($){ "use strict"; return function myscript() { alert('hello myscript'); //put all your myfile js code here } });
app/design/frontend/{Vendor}/{theme}/Magento_Theme/templates/{yourfile}.phtml
<script type="text/javascript"> require(['jquery', 'myscript'], function($, myscript) { myscript(); }); </script>
Info: don't forget to :
If Issue Solved, Click Kudos/Accept As solutions.