Skip to content

Instantly share code, notes, and snippets.

@jimkang
Created October 28, 2013 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimkang/7189773 to your computer and use it in GitHub Desktop.
Save jimkang/7189773 to your computer and use it in GitHub Desktop.
If you have more than one TypeKit that you need to select at runtime, this function is a way to do that. This uses d3 but is easily convertible to plain JS.
// Adds a script tag using the given TypeKit url. When the TypeKit script
// arrives, it loads the TypeKit and calls the callback.
function loadATypeKit(typekitURL, done) {
var head = d3.select('head');
var typekitScript = head.append('script').attr({
type: 'text/javascript',
src: typekitURL
});
var typekitScriptEl = typekitScript.node();
typekitScriptEl.onload = function loadTypeKit() {
try {
Typekit.load({
active: done,
inactive: done
});
}
catch (e) {
debugger;
}
};
}
function loadARandomTypeKit() {
if (Math.floor(Math.random() / 2) === 0) {
loadATypeKit('//use.typekit.net/aaaaaaa.js', doneLoadingTypeKit);
}
else {
loadATypeKit('//use.typekit.net/bbbbbbb.js', doneLoadingTypeKit);
}
}
function doneLoadingTypeKit() {
console.log('done!');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment