Last active
June 15, 2016 13:24
-
-
Save marcelweder/d516ea567d35ac94366c6dc4ec20d045 to your computer and use it in GitHub Desktop.
Load a CSS files asynchronously
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
/*! loadCSS: load a CSS file asynchronously. [c]2016 @scottjehl, Filament Group, Inc. Licensed MIT */ | |
!function(e) { | |
"use strict"; | |
var t = function(t, n, i) { | |
var o, a = e.document, | |
d = a.createElement("link"), | |
r = i || "all"; | |
if (n) { | |
o = n; | |
} else { | |
var l = (a.body || a.getElementsByTagName("head")[0]).childNodes; | |
o = l[l.length - 1]; | |
} | |
var s = a.styleSheets; | |
d.rel = "stylesheet", d.href = t, d.media = "only x", o.parentNode.insertBefore(d, n ? o : o.nextSibling); | |
var f = function(e) { | |
for (var t = d.href, n = s.length; n--;) { | |
if (s[n].href === t) { | |
return e(); | |
} | |
} | |
setTimeout(function() { | |
f(e); | |
}); | |
}; | |
return d.addEventListener && d.addEventListener("load", function() { | |
this.media = r; | |
}), d.onloadcssdefined = f, f(function() { | |
d.media !== r && (d.media = r); | |
}), d; | |
}; | |
"undefined" !== typeof exports ? exports.loadCSS = t : e.loadCSS = t; | |
}("undefined" !== typeof global ? global : this); | |
/* CSS rel=preload polyfill (from src/cssrelpreload.js) */ | |
(function(w) { | |
"use strict"; | |
// rel=preload support test | |
function support() { | |
try { | |
return w.document.createElement("link").relList.supports("preload"); | |
} catch (e) {} | |
} | |
// loop preload links and fetch using loadCSS | |
function poly() { | |
var links = w.document.getElementsByTagName("link"); | |
for (var i = 0; i < links.length; i++) { | |
var link = links[i]; | |
if (link.rel === "preload" && link.getAttribute("as") === "style") { | |
w.loadCSS(link.href, link); | |
link.rel = null; | |
} | |
} | |
} | |
// if link[rel=preload] is not supported, we must fetch the CSS manually using loadCSS | |
if (!support()) { | |
poly(); | |
var run = w.setInterval(poly, 300); | |
if (w.addEventListener) { | |
w.addEventListener("load", function() { | |
w.clearInterval(run); | |
}); | |
} | |
} | |
}(this)); | |
(function(w) { | |
'use strict'; | |
var doc = document, | |
head = doc.getElementsByTagName('head')[0], | |
s = 'string', | |
f = false, | |
push = 'push', | |
readyState = 'readyState', | |
onreadystatechange = 'onreadystatechange', | |
list = {}, | |
ids = {}, | |
delay = {}, | |
scripts = {}, | |
scriptpath, | |
urlArgs; | |
var jsElements = doc.querySelectorAll('[data-js-src]'), | |
jsCores = [], | |
jsFinals = []; | |
function every(ar, fn) { | |
for (var i = 0, j = ar.length; i < j; ++i) { | |
if (!fn(ar[i])) { | |
return f; | |
} | |
} | |
return 1; | |
} | |
function each(ar, fn) { | |
every(ar, function(el) { | |
return !fn(el); | |
}); | |
} | |
function create(path, fn) { | |
var el = doc.createElement('script'), | |
loaded; | |
el.onload = el.onerror = el[onreadystatechange] = function() { | |
if ((el[readyState] && !(/^c|loade/.test(el[readyState]))) || loaded) { | |
return; | |
} | |
el.onload = el[onreadystatechange] = null; | |
loaded = 1; | |
scripts[path] = 2; | |
fn(); | |
}; | |
// el.async = 1; | |
el.src = urlArgs ? path + (path.indexOf('?') === -1 ? '?' : '&') + urlArgs : path; | |
doc.body.appendChild(el); | |
// head.insertBefore(el, head.lastChild); | |
} | |
function loadJS(paths, idOrDone, optDone) { | |
paths = paths[push] ? paths : [paths]; | |
var idOrDoneIsDone = idOrDone && idOrDone.call, | |
done = idOrDoneIsDone ? idOrDone : optDone, | |
id = idOrDoneIsDone ? paths.join('') : idOrDone, | |
queue = paths.length; | |
function loopFn(item) { | |
return item.call ? item() : list[item]; | |
} | |
function callback() { | |
if (!--queue) { | |
list[id] = 1; | |
done && done(); | |
for (var dset in delay) { | |
every(dset.split('|'), loopFn) && !each(delay[dset], loopFn) && (delay[dset] = []); | |
} | |
} | |
} | |
setTimeout(function() { | |
each(paths, function loading(path, force) { | |
if (path === null) { | |
return callback(); | |
} | |
if (!force && !/^https?:\/\//.test(path) && scriptpath) { | |
path = (path.indexOf('.js') === -1) ? scriptpath + path + '.js' : scriptpath + path; | |
} | |
if (scripts[path]) { | |
if (id) { | |
ids[id] = 1; | |
} | |
return (scripts[path] === 2) ? callback() : setTimeout(function() { loading(path, true); }, 0); | |
} | |
scripts[path] = 1; | |
if (id) { | |
ids[id] = 1; | |
} | |
create(path, callback); | |
}); | |
}, 0); | |
return loadJS; | |
} | |
loadJS.get = create; | |
loadJS.order = function(scripts, id, done) { | |
(function callback(s) { | |
s = scripts.shift(); | |
!scripts.length ? loadJS(s, id, done) : loadJS(s, callback); | |
}()); | |
}; | |
loadJS.done = function(idOrDone) { | |
loadJS([null], idOrDone); | |
}; | |
loadJS.path = function(p) { | |
scriptpath = p; | |
}; | |
loadJS.urlArgs = function(str) { | |
urlArgs = str; | |
}; | |
loadJS.ready = function(deps, ready, req) { | |
deps = deps[push] ? deps : [deps]; | |
var missing = []; | |
!each(deps, function(dep) { | |
list[dep] || missing[push](dep); | |
}) && every(deps, function(dep) { | |
return list[dep]; | |
}) ? | |
ready() : ! function(key) { | |
delay[key] = delay[key] || []; | |
delay[key][push](ready); | |
req && req(missing); | |
}(deps.join('|')); | |
return loadJS; | |
}; | |
if (jsElements.length) { | |
var idx = 0; | |
for (idx = 0; idx < jsElements.length; idx++) { | |
var attrSrc = jsElements[idx].getAttribute('data-js-src'), | |
attrDep = jsElements[idx].getAttribute('data-js-dep'); | |
if (attrDep !== 'korrekt') { | |
jsFinals.push(attrSrc); | |
} else { | |
jsCores.push(attrSrc); | |
} | |
} | |
loadJS(jsCores, function() { | |
loadJS(jsFinals); | |
}); | |
} | |
}(this)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment