How to include JavaScript from a CDN in meteor?

后端 未结 3 1053
孤城傲影
孤城傲影 2021-02-05 06:04

I\'d like to include JS from a CDN in Meteor before including my own client scripts so that the client scripts can depend on it.

...


        
3条回答
  •  我在风中等你
    2021-02-05 06:28

    Assuming you don't need to load these files before the Meteor packages, create a JS file which is loaded before any of the others. Meteor loads files in alphabetical order so it must be the first file loaded. To that end, naming it aaLoadCDN.js should suffice. Dynamically load the CDN scripts by adding a script src element to the document head:

    var script = document.createElement('script');
    script.setAttribute('type', 'text/javascript');  // optional
    script.setAttribute('src', 'url/to/the/cdn/script.js');
    document.getElementsByTagName('head')[0].appendChild(script);
    

    Here are some real-world Meteor packages loading scripts from CDNs:

    • snapsvg
    • Font-Awesome (CSS).

提交回复
热议问题