Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit cafae99

Browse files
committed
first pass at a css rel=preload polyfill workflow. Note: expects loadCSS function to be defined. #59
1 parent dc81c5c commit cafae99

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/cssrelpreload.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* CSS rel=preload polyfill. Depends on loadCSS function */
2+
(function( w ){
3+
// rel=preload support test
4+
function support(){
5+
try {
6+
return w.document.createElement("link").relList.supports( "preload" );
7+
} catch (e) {}
8+
}
9+
// loop preload links and fetch using loadCSS
10+
function poly(){
11+
var links = w.document.getElementsByTagName( "link" );
12+
for( var i = 0; i < links.length; i++ ){
13+
if( links[ i ].getAttribute( "rel" ) === "preload" ){
14+
w.loadCSS( links[ i ].href, links[ i ] );
15+
links[ i ].rel = null;
16+
}
17+
}
18+
}
19+
// if link[rel=preload] is not supported, we must fetch the CSS manually using loadCSS
20+
if( !support() ){
21+
poly();
22+
var run = w.setInterval( poly, 300 );
23+
if( w.addEventListener ){
24+
w.addEventListener( "load", function(){
25+
w.clearInterval( run );
26+
} )
27+
}
28+
}
29+
}( this ));

0 commit comments

Comments
 (0)