├── tinywfl-min.js ├── LICENSE ├── tinywfl.js └── README.md /tinywfl-min.js: -------------------------------------------------------------------------------- 1 | /*v1.0.0*/function TinyWFL(e,t){function n(){for(f=(new Date).getTime(),l=0;l=u)return i();for(r=1,l=0;l= pollTimeout) { 60 | // Failed to load the font 61 | return clean(); 62 | } 63 | 64 | // Test for no change 65 | allLoaded = 1; 66 | for (i=0; i`` tag when your fonts have loaded, 10 | and call an optional callback function if you want to know about it in 11 | JavaScript. It is self-contained and has no dependencies. 12 | 13 | Version 1.0.0 14 | 15 | 16 | ## Usage 17 | 18 | 1. Load the script to add the global function TinyWFL, then call it with a list 19 | of web font names: 20 | 21 | 22 | 25 | 26 | The order of the fonts doesn't matter. 27 | 28 | This code could go anywhere in your page, but TinyWFL will work best if this 29 | happens in the ```` - that way it can immediately disable your web 30 | fonts and try to re-enable them while the rest of the page loads, hopefully 31 | eliminating FOIT/FOUT altogether. 32 | 33 | 2. Now define your ``@font-face`` in CSS, using the font-face definition as 34 | supplied with your web font: 35 | 36 | @font-face { 37 | font-family: 'Courgette'; 38 | src: url('fonts/courgette.eot'); 39 | src: url('fonts/courgette.eot?#iefix') format('embedded-opentype'), 40 | url('fonts/courgette.woff') format('woff'), 41 | url('fonts/courgette.ttf') format('truetype'); 42 | } 43 | 44 | Alternatively if you are using a hosted font service, add their stylesheet 45 | to your HTML: 46 | 47 | 48 | 49 | 3. Now set up your CSS to use your fallback fonts when TinyWFL is running but 50 | your fonts are not yet available: 51 | 52 | /* Use your web font by default */ 53 | body { 54 | font-family: "Open Sans", Verdana, sans-serif; 55 | } 56 | 57 | /* Turn it off when TinyWFL is present, but the font isn't available yet */ 58 | html.tinywfl:not(.tinywfl-opensans) body { 59 | font-family: Verdana, sans-serif; 60 | } 61 | 62 | With that arrangement your web fonts can still be used if TinyWFL fails to 63 | load (eg if JavaScript isn't available) - albeit with a FOUT. 64 | 65 | 66 | ### Callbacks 67 | 68 | You can also take actions when JavaScript loads by passing a callback function: 69 | 70 | TinyWFL(['Courgette', 'Open Sans'], resizeDoc); 71 | 72 | The callback function will be called each time a font is loaded. If you need to 73 | know which font was loaded, check the first argument: 74 | 75 | // Example callback function 76 | function resizeDoc(fontName) { 77 | resizeHeader(); 78 | if (fontName === 'Open Sans') { 79 | resizeBody(); 80 | } 81 | } 82 | 83 | 84 | ## How it works 85 | 86 | TinyWFL doesn't actually do any font loading itself - you still need to add the 87 | CSS to specify your font family, as you would without TinyWFL. That way your 88 | web fonts will still work when JavaScript is disabled or fails to load for some 89 | reason. 90 | 91 | Instead, TinyWFL manages the loading process; it watches to see when the fonts 92 | have loaded, and sets classes on ```` to allow your CSS to change based 93 | on which fonts are available. 94 | 95 | When it starts up, TinyWFL adds the ``twfl`` class to ````, then as 96 | each of your fonts is loaded it will add a ``twfl-`` class, 97 | where ```` is a modified version of the font name you supplied - 98 | it is in lowercase, with any characters other than ``a-z``, ``0-9`` and ``-`` 99 | or ``_`` removed. 100 | 101 | For example, if your font is called ``Badger #27``, when it has loaded TinyWFL 102 | will add the class ``twfl-badger27`` to ````. 103 | 104 | 105 | For more information about TinyWFL and why it exists, see the blog post 106 | [A Tiny Web Font Loader](http://radiac.net/blog/2015/09/tiny-web-font-loader/). 107 | 108 | 109 | ## Contributing 110 | 111 | Contributions are welcome, preferably via pull request. However, given the 112 | focus of the project is to be as small as possible, bug fixes are preferred 113 | to new features. 114 | 115 | ### Credits 116 | 117 | Although originally developed independently, TinyWFL has shamelessly 118 | incorporated parts of webfontloader over the years to become a better WFL - 119 | most notably the test element's CSS and the test string. 120 | --------------------------------------------------------------------------------