I've created a custom widget with a popup on homepage loading, now i need a function to close the popup but i'm not able to include a javascript file in the widget. I've tried the solution that i read in other post but i haven't find the solution yet.

Magento version: 2.1.7

Hope someone can help me, thank you in advance.

  • when you say a 'custom widget' do you mean that you created a widget type other then the default ones that you select in the dropdown when you make a new widget? In most cases the widget is pulling in a static block that you can include your js in, are you doing something else here?
    – circlesix
    Aug 9, 2017 at 1:37
  • I've created another widget type, i'm using it only for the popup Aug 9, 2017 at 9:39
  • could you include the code you used to create it?
    – circlesix
    Aug 9, 2017 at 13:36

2 Answers 2


Create requirejs-config

var config = {
    map: {
        '*': {
            yourWidgetName: 'NameSpace_ModuleName/js/yourwidgetfile',  

Define you widget following convention

], function($) {
    "use strict";

    $.widget('namespace.yourWidgetName', {
      // Logicial code

      // Optional 
      options: {
         // Define variables

      _create: function() {
         // Init code

      closeModal: function() {
         // Do close modal here


    return $.namespace.yourWidgetName;

For use widget you can call it anywhere if it defined. Such as in template

<div data-mage-init='{"yourWidgetName": {}}' class="container-wrapper" />

I've resolved the problem using the following script:

    ) {
        var options = {
            type: 'popup',
            responsive: true,
            innerScroll: true,
            title: 'Iscriviti al nostro sito',
            buttons: [{
                text: $.mage.__('Continue'),
                class: '',
                click: function () {

        var popup = modal(options, $('#popup-modal'));


Anyway thank you for the help, i hope this can be useful for other users

Your Answer

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

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