Created
October 28, 2013 00:51
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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