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

Commit

Permalink
update enhance.js to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
scottjehl committed Oct 24, 2014
1 parent 654bacb commit bc3b60e
Showing 1 changed file with 28 additions and 5 deletions.
33 changes: 28 additions & 5 deletions _includes/enhance.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
var ref = window.document.getElementsByTagName( "script" )[ 0 ];
var script = window.document.createElement( "script" );
script.src = src;
script.async = true;
ref.parentNode.insertBefore( script, ref );
return script;
}
Expand All @@ -36,18 +37,40 @@

// loadCSS: load a CSS file asynchronously. Included from https://github.com/filamentgroup/loadCSS/
function loadCSS( href, before, media ){
// Arguments explained:
// `href` is the URL for your CSS file.
// `before` optionally defines the element we'll use as a reference for injecting our <link>
// By default, `before` uses the first <script> element in the page.
// However, since the order in which stylesheets are referenced matters, you might need a more specific location in your document.
// If so, pass a different reference element to the `before` argument and it'll insert before that instead
// note: `insertBefore` is used instead of `appendChild`, for safety re: http://www.paulirish.com/2011/surefire-dom-element-insertion/
var ss = window.document.createElement( "link" );
var ref = before || window.document.getElementsByTagName( "script" )[ 0 ];
var sheets = window.document.styleSheets;
ss.rel = "stylesheet";
ss.href = href;
// temporarily, set media to something non-matching to ensure it'll fetch without blocking render
ss.media = "only x";
// inject link
ref.parentNode.insertBefore( ss, ref );
// set media back to `all` so that the styleshet applies once it loads
setTimeout( function(){
ss.media = media || "all";
} );
// This function sets the link's media back to `all` so that the stylesheet applies once it loads
// It is designed to poll until document.styleSheets includes the new sheet.
function toggleMedia(){
var defined;
for( var i = 0; i < sheets.length; i++ ){
if( sheets[ i ].href && sheets[ i ].href.indexOf( href ) > -1 ){
defined = true;
}
}
if( defined ){
ss.media = media || "all";
}
else {
setTimeout( toggleMedia );
}
}

toggleMedia();
return ss;
}

Expand Down Expand Up @@ -152,4 +175,4 @@
loadCSS( fonts.content );
}

}( this ));
}( this ));

0 comments on commit bc3b60e

Please sign in to comment.