Skip to content

Instantly share code, notes, and snippets.

@jplhomer
Last active December 20, 2017 18:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jplhomer/d60e74681bf133ae006f to your computer and use it in GitHub Desktop.
Save jplhomer/d60e74681bf133ae006f to your computer and use it in GitHub Desktop.
Example of loading fonts with loadCSS
<!-- Our font file is fonts.css -->
<script>
/*!
loadCSS: load a CSS file asynchronously.
[c]2014 @scottjehl, Filament Group, Inc.
Licensed MIT
*/
/* exported loadCSS */
function loadCSS( href, before, media, callback ){
"use strict";
// 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";
// DEPRECATED
if( callback ) {
ss.onload = callback;
}
// inject link
ref.parentNode.insertBefore( ss, ref );
// 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.
ss.onloadcssdefined = function( cb ){
var defined;
for( var i = 0; i < sheets.length; i++ ){
if( sheets[ i ].href && sheets[ i ].href === ss.href ){
defined = true;
}
}
if( defined ){
cb();
} else {
setTimeout(function() {
ss.onloadcssdefined( cb );
});
}
};
ss.onloadcssdefined(function() {
ss.media = media || "all";
});
return ss;
}
// use loadCSS to load fonts.css
loadCSS('/fonts.css');
</script>
<!-- Provide a fallback in case JavaScript is broken -->
<noscript>
<link href="/fonts.css" rel="stylesheet" />
</noscript>
<!-- Here's an example with Google Web Fonts: Open Sans -->
<script>
/*!
loadCSS: load a CSS file asynchronously.
[c]2014 @scottjehl, Filament Group, Inc.
Licensed MIT
*/
/* exported loadCSS */
function loadCSS( href, before, media, callback ){
"use strict";
// 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";
// DEPRECATED
if( callback ) {
ss.onload = callback;
}
// inject link
ref.parentNode.insertBefore( ss, ref );
// 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.
ss.onloadcssdefined = function( cb ){
var defined;
for( var i = 0; i < sheets.length; i++ ){
if( sheets[ i ].href && sheets[ i ].href === ss.href ){
defined = true;
}
}
if( defined ){
cb();
} else {
setTimeout(function() {
ss.onloadcssdefined( cb );
});
}
};
ss.onloadcssdefined(function() {
ss.media = media || "all";
});
return ss;
}
// use loadCSS to load fonts.css
loadCSS('//fonts.googleapis.com/css?family=Open+Sans');
</script>
<!-- Provide a fallback in case JavaScript is broken -->
<noscript>
<link href='http://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
</noscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment