jQuery Usage in Magento 2 – How to Resolve Issues

javascriptjquerymagento2product

Currently I have a custom block loaded almost last on the page containing:

<script type="text/javascript" src="/test.js"></script>

This script is pretty much the usual stuff, just manipulating HTML of my product page.

jQuery.noConflict();
  jQuery(document).ready(function($) {
    $(window).load(function() {
    {
      Do stuff with various HTML elements;
    }
  });
});

However, this method only seems to work as intended in Chrome. In Firefox, it just throws jQuery not defined, despite Magento 2 obviously having a local copy of jQuery called very early on the page.

Any ideas on how to get this working in Firefox? (Or better yet, how to do this properly, as I have a feeling I'm skirting round a lot of Magento's intended JS usage.)

Best Answer

add js like that

require([ 'jquery', 'jquery/ui'], function($){ $(document).ready(function($) {
    $(window).load(function() {
    {
      Do stuff with various HTML elements;
    }
  });
}); });
Related Topic