Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(function() {
var a = document.getElementsByTagName('script'),
i, l, result,
rurl = /^http:\/\/use\.typekit\.com\/([0-9A-Za-z]+)\.js/;
for (i = 0, l = a.length - 1; i < l; i++) {
if (result = rurl.exec(a[i].getAttribute('src'))) {
window.location.assign('' + result[1]);
if (!result) {
alert('This site doesn\'t use Typekit!');

This comment has been minimized.

Copy link
Owner Author

@pdokas pdokas commented Nov 13, 2010

A few improvements:

  1. Wrap everything in a self-executing function to prevent polluting the global namespace.
  2. Use regex to acquire the kit ID in the src of scripts.
  3. Don't recalculate the length of the script NodeList since that's an expensive recalculation on the live DOM every time through.

This comment has been minimized.

Copy link

@capndesign capndesign commented Nov 16, 2010

Awesome! I'm definitely going to replace my code with yours. Works great!

This is also why I posted it as a gist. I knew someone who had a better-than-hacky knowledge of JS would write something better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment