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

Commit dd3e57a

Browse files
authored
Merge pull request #252 from filamentgroup/251
251
2 parents 31fe0e1 + e1081d2 commit dd3e57a

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fg-loadcss",
3-
"version": "2.0.0",
3+
"version": "2.0.1-0",
44
"description": "A function for loading CSS asynchronously",
55
"main": "src/loadCSS.js",
66
"repository": {

src/cssrelpreload.js

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,50 @@
1313
// define on the loadCSS obj
1414
var rp = loadCSS.relpreload = {};
1515
// rel=preload feature support test
16-
rp.support = function(){
16+
// runs once and returns a function for compat purposes
17+
rp.support = (function(){
18+
var ret;
1719
try {
18-
return w.document.createElement( "link" ).relList.supports( "preload" );
20+
ret = w.document.createElement( "link" ).relList.supports( "preload" );
1921
} catch (e) {
20-
return false;
22+
ret = false;
2123
}
22-
};
24+
return function(){
25+
return ret;
26+
};
27+
})();
2328

2429
// if preload isn't supported, get an asynchronous load by using a non-matching media attribute
2530
// then change that media back to its intended value on load
26-
rp.bindMediaToggle = function( link, media ){
31+
rp.bindMediaToggle = function( link ){
32+
// remember existing media attr for ultimate state, or default to 'all'
33+
var finalMedia = link.media || "all";
34+
2735
function enableStylesheet(){
28-
link.media = media;
36+
link.media = finalMedia;
2937
}
38+
39+
// bind load handlers to enable media
3040
if( link.addEventListener ){
3141
link.addEventListener( "load", enableStylesheet );
3242
} else if( link.attachEvent ){
3343
link.attachEvent( "onload", enableStylesheet );
3444
}
45+
46+
// Set rel and non-applicable media type to start an async request
47+
// note: timeout allows this to happen async to let rendering continue in IE
48+
setTimeout(function(){
49+
link.rel = "stylesheet";
50+
link.media = "only x";
51+
});
3552
// also enable media after 3 seconds,
3653
// which will catch very old browsers (android 2.x, old firefox) that don't support onload on link
3754
setTimeout( enableStylesheet, 3000 );
3855
};
3956

4057
// loop through link elements in DOM
4158
rp.poly = function(){
59+
// double check this to prevent external calls from running
4260
if( rp.support() ){
4361
return;
4462
}
@@ -47,20 +65,21 @@
4765
var link = links[ i ];
4866
// qualify links to those with rel=preload and as=style attrs
4967
if( link.rel === "preload" && link.getAttribute( "as" ) === "style" && !link.getAttribute( "data-loadcss" ) ){
50-
// remember existing media attr for ultimate state, or default to 'all'
51-
var finalMedia = link.media || "all";
52-
// bind listeners to toggle media back
53-
rp.bindMediaToggle( link, finalMedia );
54-
// if preload is not supported, kick off an asynchronous request by using a non-matching media query and rel=stylesheet
55-
link.media = "x";
56-
link.rel = "stylesheet";
57-
5868
// prevent rerunning on link
5969
link.setAttribute( "data-loadcss", true );
70+
// bind listeners to toggle media back
71+
rp.bindMediaToggle( link );
6072
}
6173
}
74+
};
75+
76+
// if unsupported, run the polyfill
77+
if( !rp.support() ){
78+
// run once at least
79+
rp.poly();
80+
6281
// rerun poly on an interval until onload
63-
var run = w.setInterval( rp.poly, 300 );
82+
var run = w.setInterval( rp.poly, 500 );
6483
if( w.addEventListener ){
6584
w.addEventListener( "load", function(){
6685
rp.poly();
@@ -72,9 +91,9 @@
7291
w.clearInterval( run );
7392
} );
7493
}
75-
};
76-
// run once at least
77-
rp.poly();
94+
}
95+
96+
7897
// commonjs
7998
if( typeof exports !== "undefined" ){
8099
exports.loadCSS = loadCSS;

0 commit comments

Comments
 (0)