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

Qaisar Satti
32.6k18 gold badges88 silver badges138 bronze badges
asked Feb 22, 2016 at 10:38

2 Answers 2

10

add js like that

require([ 'jquery', 'jquery/ui'], function($){ $(document).ready(function($) {
 $(window).load(function() {
 {
 Do stuff with various HTML elements;
 }
 });
}); });
answered Feb 22, 2016 at 10:59
4
  • Perfect, thank you so much, this had been driving me crazy. Commented Feb 22, 2016 at 11:38
  • There is a !domReady plugin included within the require implementation btw. Commented Dec 23, 2016 at 13:52
  • How to use the !domReady? window.load and document.ready seem to fire a bet prematurely for me Commented Aug 4, 2017 at 10:17
  • [ 'jquery', 'jquery/ui, 'domReady!'] is this all that is required for this? Commented Aug 4, 2017 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
 });
 });
answered Dec 23, 2016 at 10:25
1

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.