Magento – $.widget is not a function when loading a homepage after add custom jQuery on custom theme

jquerymagento2theme

$.widget is not a function when loading a homepage after add custom jQuery on custom theme.

I have define my custom jQuery javascript library on my theme header under

Magento_Theme/layout/default_head_blocks.xml

The js error message

$.wedget is not a function

which occur when loading homepage sometime ….[not 100% occur].

Is about js definition location problem? please help.

For example one of my jQuery source code:

jQuery(window).scroll(function(){
  var sticky = jQuery('.page-header'),
      scroll = jQuery(window).scrollTop(); 
  if (scroll >= 30) sticky.addClass('fixed'), jQuery('.panel.header').hide(),jQuery('.page-header').css('border-bottom','1px solid #efefef');
  else sticky.removeClass('fixed'),jQuery('.panel.header').show(),jQuery('.page-header').css('border-bottom','none');
});

Best Answer

First of all check jQuery is working or not using following code:

<script type="text/javascript">
require(['jquery'],function($){
    $(window).load(function() {
        alert('jquery working');
        // your code goes here.
    });

});
</script>

If it's work then put your code inside this code section.Also load your js file after jQuery library.

Related Topic