0

I am trying to load an external javascript in my Magento 2.1.x installation in this way:

    <script type="text/javascript">

        var _iub = _iub || [];
        _iub.csConfiguration = {
            siteId: xxx,
            consentOnScrollDelay : 1500,
            callback: {
                onConsentGiven: function(){
                    dataLayer.push({'event': 'iubenda_consent_given'});
                }
            },
            cookiePolicyId: xxx,
            lang: "it",
            banner: {
                fontSize: "13", applyStyles: false,
                html:"Privacy policy HTML"
            }
        };
    </script>
    <script type="text/javascript" src="//cdn.iubenda.com/cookie_solution/safemode/iubenda_cs.js" charset="UTF-8" async></script>

Including the script //cdn.iubenda.com/cookie_solution/safemode/iubenda_cs.js breaks all the other javascript requirejs functionalities.

I tried to include the script in the requirejs way, editing my theme requirejs-config.js file:

var config = {
    "map" : {
        "*" : {
            ...
            'iubenda' : '//cdn.iubenda.com/cookie_solution/safemode/iubenda_cs.js'
        }
    }
};

and changing its inclusion in this way

<script type="text/javascript">
        var _iub = _iub || [];
        _iub.csConfiguration = {
            siteId: xxx,
            consentOnScrollDelay : 1500,
            callback: {
                onConsentGiven: function(){
                    dataLayer.push({'event': 'iubenda_consent_given'});
                }
            },
            cookiePolicyId: xxx,
            lang: "it",
            banner: {
                fontSize: "13", applyStyles: false,
                html:"Privacy policy HTML"
            }
        };
    </script>

    <script>
        require(['iubenda'], function(){});
    </script>

But this does not solve the errors.

The common error thrown is

Uncaught Error: Mismatched anonymous define() module: function (){return l}

How can I add external non AMD scripts to my Magento 2.1.x without breaking the requirejs functionalities?

0

You must create the javascript as follows:

<script>
    var _iub = _iub || [];
    _iub.csConfiguration = {
        siteId: xxx,
        consentOnScrollDelay: 1500,
        callback: {
            onConsentGiven: function () {
                dataLayer.push({'event': 'iubenda_consent_given'});
            }
        },
        cookiePolicyId: xxx,
        lang: "it",
        banner: {
            fontSize: "13", applyStyles: false,
            html: "Privacy policy HTML"
        }
    };
    $.getScript("//cdn.iubenda.com/cookie_solution/safemode/iubenda_cs.js");
</script>

You must remove ... requirejs-config.js

Old:

var config = {
    "map" : {
        "*" : {
            ...
            'iubenda' : '//cdn.iubenda.com/cookie_solution/safemode/iubenda_cs.js'
        }
    }
};

New:

var config = {
    "map" : {
        "*" : {
            'iubenda' : '//cdn.iubenda.com/cookie_solution/safemode/iubenda_cs.js'
        }
    }
};
  • Doing this won't solve my problem. Requiring 'iubenda' script causes the breaking of other scripts. In additon, the code you include after having required iubenda must be before the require. – gianis6 Oct 7 '17 at 9:14
  • I updated my answer, Try. – St3phan Oct 7 '17 at 9:23
  • Sorry, but this also does not solve my problems. The JS errors are still there – gianis6 Oct 7 '17 at 9:29
  • Please fill in your question with the error you receive. – St3phan Oct 7 '17 at 9:31
  • see my edited answer – gianis6 Oct 7 '17 at 9:34

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.