-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfontloader.js
More file actions
49 lines (41 loc) · 1.71 KB
/
fontloader.js
File metadata and controls
49 lines (41 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/* Load fonts as quickly as possible, but also without blocking other stuff */
/* Original code by Adam Beres-Deak, modified by Paul Rayes */
/* https://github.com/bdadam/OptimizedWebfontLoading */
//This script must be placed in the HEAD above all external stylesheet declarations (link[rel=stylesheet])
(function f(modifiedDate) {
// 0. Many unsupported browsers should stop here
var nua = navigator.userAgent;
if (
!window.addEventListener // IE8 and below
|| (nua.match(/(Android (2|3|4.0|4.1|4.2|4.3))|(Opera (Mini|Mobi))/) && !nua.match(/Chrome/)) // Android Stock Browser below 4.4 and Opera Mini
) {
return;
}
// 2. Setting up the <style> element, that we are using to apply the base64 encoded font data
var styleElement = document.createElement('style');
//styleElement.rel = 'stylesheet';
document.head.appendChild(styleElement);
// Setting styleElement.textContent must be after this line, because of IE9 errors
// 5. Checking for WOFF2 support to know which URL we should use
var url = '/fonts/fonts.woff' + (supportsWoff2() ? '2' : '') + '.css?' + modifiedDate;
// 6. Fetching the font data from the server
var request = new XMLHttpRequest();
request.open('GET', url);
request.onload = function() {
if (request.status >= 200 && request.status < 400) {
// 4. Applying the font style sheet
styleElement.textContent = request.responseText;
}
};
request.send();
function supportsWoff2() {
// Source: https://github.com/filamentgroup/woff2-feature-test
if (!window.FontFace) {
return false;
}
var f = new FontFace('t', 'url("data:application/font-woff2,") format("woff2")', {});
var p = f.load();
try {p.then(null, function(){});}catch(e){}
return f.status === 'loading';
}
})