- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019
10:40 PM
01-29-2019
10:40 PM
What am I missing?
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
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019
10:45 PM
01-29-2019
10:45 PM
Re: What am I missing?
Hi .
Magento 2 use RequireJs . You should not add js directly to xml file .
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019
11:40 PM
01-29-2019
11:40 PM
Re: What am I missing?
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 :
- clean the cache
- clean var/view_preprocessed content
- clean pub/static content
- deploy the static content = php bin/magento setup:static-content:deploy -f
If Issue Solved, Click Kudos/Accept As solutions.