javascriptwebfont-loader

Webfont Loader in modular js pattern


The webfont loader docs provide this example script:

WebFontConfig = {
  typekit: { id: 'xxxxxx' }
};

(function(d) {
  var wf = d.createElement('script'), s = d.scripts[0];
  wf.src = 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js';
  s.parentNode.insertBefore(wf, s);
})(document);

If I put that directly in a script tag in the <head> of my page it works fine. If I wrap it in a function and try to export that, I get this error in the console:

Uncaught ReferenceError: WebFontConfig is not defined

Here's how I attempted to wrap it:

function initialiseWebFontLoader() {
    WebFontConfig = {
        google: {
            families: ['Open Sans', 'Lora']
        },
        timeout: 1500
    };

    (function(d) {
        const wf = d.createElement('script'), s = d.scripts[0];
        wf.src = 'https://ajax.googleapis.com/ajax/libs/webfont/1.6.16/webfont.js';
        s.parentNode.insertBefore(wf, s);
    })(document);
}

module.exports = {
    initialiseWebFontLoader: initialiseWebFontLoader
};

Am I doing something obviously wrong here?


Solution

  • Oh, I needed to attach WebFontConfig to the window object.

    window.WebFontConfig.