5

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.)

9

add js like that

require([ 'jquery', 'jquery/ui'], function($){ $(document).ready(function($) {
    $(window).load(function() {
    {
      Do stuff with various HTML elements;
    }
  });
}); });
  • Perfect, thank you so much, this had been driving me crazy. – Niall Storie Feb 22 '16 at 11:38
  • There is a !domReady plugin included within the require implementation btw. – Ben Crook Dec 23 '16 at 13:52
  • How to use the !domReady? window.load and document.ready seem to fire a bet prematurely for me – harri Aug 4 '17 at 10:17
  • [ 'jquery', 'jquery/ui, 'domReady!'] is this all that is required for this? – harri Aug 4 '17 at 10:25
0

You can use your custom jquery function by:

 require([ 'jquery', 'jquery/ui'], function($){ 
      $(document).ready(function($) {
           alert("Hello"); //Do Your jQuery Code
      });
 });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.