1//This script must be placed in the HEAD above all external stylesheet declarations (link[rel=stylesheet]) 2function loadFont(fontName, woffUrl, woff2Url, onlyLoadFontOnSecondPageload) { 3 // 1. Setting up localStorage 4 const loSto = localStorage || {}, 5 localStoragePrefix = 'x-font-' + fontName, 6 localStorageUrlKey = localStoragePrefix + 'url', 7 localStorageCssKey = localStoragePrefix + 'css', 8 storedFontUrl = loSto[localStorageUrlKey], 9 storedFontCss = loSto[localStorageCssKey]; 10 11 // 2. Setting up the <style> element, that we are using to apply the base64 encoded font data 12 let styleElement = document.createElement('style'); 13 styleElement.rel = 'stylesheet'; 14 document.head.appendChild(styleElement); 15 // Setting styleElement.textContent must be after this line, because of IE9 errors 16 17 // 3. Checking whether the font data is already in localStorage and up-to-date 18 if (storedFontCss && (storedFontUrl === woffUrl || storedFontUrl === woff2Url)) { 19 // the css is still in the localStorage 20 // AND it was loaded from one of the current URLs 21 22 // 4. Applying the font style sheet 23 styleElement.textContent = storedFontCss; 24 25 } else { 26 // The data was not present, or loaded from an obsolete URL 27 // So we have to load it again 28 29 // 5. Checking for WOFF2 support to know which URL we should use 30 const url = (woff2Url) 31 ? woff2Url // WOFF2 URL provided and supported 32 : woffUrl; // only WOFF support 33 34 // 6. Fetching the font data from the server 35 const request = new XMLHttpRequest(); 36 request.open('GET', url); 37 request.onload = function() { 38 if (request.status >= 200 && request.status < 400) { 39 // 7. Updating localStorage with the fresh data and applying the font data 40 loSto[localStorageUrlKey] = url; 41 loSto[localStorageCssKey] = request.responseText; 42 if (!onlyLoadFontOnSecondPageload) { 43 styleElement.textContent = request.responseText; 44 } 45 } 46 }; 47 request.send(); 48 } 49}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.