|
21 | 21 | } |
22 | 22 | }; |
23 | 23 |
|
| 24 | + // if preload isn't supported, get an asynchronous load by using a non-matching media attribute |
| 25 | + // then change that media back to its intended value on load |
| 26 | + rp.bindMediaToggle = function( link, media ){ |
| 27 | + function enableStylesheet(){ |
| 28 | + link.media = media; |
| 29 | + } |
| 30 | + if( link.addEventListener ){ |
| 31 | + link.addEventListener( "load", enableStylesheet ); |
| 32 | + } else if( link.attachEvent ){ |
| 33 | + link.attachEvent( "onload", enableStylesheet ); |
| 34 | + } |
| 35 | + // also enable media after 3 seconds, |
| 36 | + // which will catch very old browsers (android 2.x, old firefox) that don't support onload on link |
| 37 | + setTimeout( enableStylesheet, 3000 ); |
| 38 | + }; |
| 39 | + |
24 | 40 | // loop through link elements in DOM |
25 | 41 | rp.poly = function(){ |
26 | 42 | if( rp.support() ){ |
|
33 | 49 | if( link.rel === "preload" && link.getAttribute( "as" ) === "style" && !link.getAttribute( "data-loadcss" ) ){ |
34 | 50 | // remember existing media attr for ultimate state, or default to 'all' |
35 | 51 | var finalMedia = link.media || "all"; |
36 | | - // if preload isn't supported, get an asynchronous load by using a non-matching media attribute |
37 | | - // then change that media back to its intended value on load |
38 | | - var enableStylesheet = function(){ |
39 | | - link.media = finalMedia; |
40 | | - } |
41 | | - if( link.addEventListener ){ |
42 | | - link.addEventListener( "load", enableStylesheet ); |
43 | | - } else if( link.attachEvent ){ |
44 | | - link.attachEvent( "onload", enableStylesheet ); |
45 | | - } |
| 52 | + // bind listeners to toggle media back |
| 53 | + rp.bindMediaToggle( link, finalMedia ); |
46 | 54 | // if preload is not supported, kick off an asynchronous request by using a non-matching media query and rel=stylesheet |
47 | 55 | link.media = "x"; |
48 | 56 | link.rel = "stylesheet"; |
49 | | - // set rel=preload to stylesheet after 3 seconds, |
50 | | - // which will catch very old browsers (android 2.x, old firefox) that don't support onload on link |
51 | | - setTimeout( enableStylesheet, 3000 ); |
| 57 | + |
52 | 58 | // prevent rerunning on link |
53 | 59 | link.setAttribute( "data-loadcss", true ); |
54 | 60 | } |
|
0 commit comments