1

I've been trying to add a custom script to my Magento stores, however I can't get it to load. I've created the js file in my web/js folder in my theme, and added it to the theme's requirejs file. It will also load the file into the static folder eventually, but it doesn't work at all.

This is the js file I've been trying to load for testing purposes:

define([
'jquery',
'jquery/ui'], function($) {
alert('start openMenu');
$.widget('rvdk.openMenu', {
    _create: function() {
        alert('open-menu.js');
        console.log('open-menu.js');

        $(window).on("scroll", function () {
            alert('open-menu-scroll.js');
        });
    }
});

return $.rvdk.openMenu;

});

requirejs-config:

var config = {
map: {
    '*': {
        'open-menu'             : 'js/open-menu',
        'header'                : 'js/header',
        'products-masonry'      : 'js/products-masonry',
        'image-grid'            : 'js/image-grid',
    }
}};

It loads a few more files, but I don't think those are important. Header.js works fine for example, and I can change this file and the changes will be displayed. However it still doesn't load open-menu.js, even though it is in the static files.

2
  • You need to add "open-menu" in define([ 'jquery', 'jquery/ui','open-menu'], function($) { Your code });
    – aravind
    Jun 17, 2019 at 11:06
  • I've tried adding open-menu into the define section, but that didn't seem to make a difference.
    – Syne
    Jun 21, 2019 at 8:32

1 Answer 1

1

You can use below link.

https://inchoo.net/magento-2/custom-javascript-in-magento-2-with-requirejs/

I hope it's usefully for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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