cancel
Showing results for 
Search instead for 
Did you mean: 

How to integrate third party javascript libraries from CDN

How to integrate third party javascript libraries from CDN

Hello!

 

I was looking all day but couldn´t find a proper solution or even a statement if this is intended or not.

We use Magento 2.3.4 and I want to use a third party java script library which is hosted in the cloud. 

I was looking here: https://devdocs.magento.com/guides/v2.3/javascript-dev-guide/javascript/js-resources.html but it only mentions resources on the server. 

 

Right now I have a require-statement in the html and later I use a script tag 

 

So first comes

require(["https://cloud.linktoLibrary.js"]);

function loadCss(url) {
var link = document.createElement("link");
link.type = "text/css";
link.rel = "stylesheet";
link.href = url;
document.getElementsByTagName("head")[0].appendChild(link);
}

and later I have:

<script type="text/javascript" charset="utf-8" async="" data-requirecontext="_" data-requiremodule="https://cloud.linktoLibrary.js" src="https://cloud.linktoLibrary.js"></script>

But this causes seemingly random error messages appear in the console like X is not defined or theme.js:25 Uncaught TypeError: $(...).mage is not a function etc.

 

However the library still does what it should.

If someone has a best practice approach for this I would very much appreciate it!