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

Commit

Permalink
better before reference for insert location. now working in supported…
Browse files Browse the repository at this point in the history
… and unsupported browsers. #59
  • Loading branch information
scottjehl committed Jan 26, 2016
1 parent cafae99 commit be6e432
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
7 changes: 4 additions & 3 deletions src/cssrelpreload.js
Expand Up @@ -10,9 +10,10 @@
function poly(){
var links = w.document.getElementsByTagName( "link" );
for( var i = 0; i < links.length; i++ ){
if( links[ i ].getAttribute( "rel" ) === "preload" ){
w.loadCSS( links[ i ].href, links[ i ] );
links[ i ].rel = null;
var link = links[ i ];
if( link.rel === "preload" ){
loadCSS( link.href, link );
link.rel = null;
}
}
}
Expand Down
13 changes: 8 additions & 5 deletions test/preload.html
Expand Up @@ -6,8 +6,10 @@

<link rel="preload" href="http://scottjehl.com/css-temp/slow.php" as="style" onload="this.rel='stylesheet'">
<script>
/*! 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 */
/* CSS rel=preload polyfill (from src/cssrelpreload.js) */
(function( w ){
// rel=preload support test
function support(){
Expand All @@ -19,9 +21,10 @@
function poly(){
var links = w.document.getElementsByTagName( "link" );
for( var i = 0; i < links.length; i++ ){
if( links[ i ].getAttribute( "rel" ) === "preload" ){
w.loadCSS( links[ i ].href, links[ i ] );
links[ i ].rel = null;
var link = links[ i ];
if( link.rel === "preload" ){
loadCSS( link.href, link );
link.rel = null;
}
}
}
Expand All @@ -32,7 +35,7 @@
if( w.addEventListener ){
w.addEventListener( "load", function(){
w.clearInterval( run );
} )
} );
}
}
}( this ));
Expand Down

0 comments on commit be6e432

Please sign in to comment.