Skip to content

Instantly share code, notes, and snippets.

@mzabriskie
Created April 3, 2013 20:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzabriskie/5304777 to your computer and use it in GitHub Desktop.
Save mzabriskie/5304777 to your computer and use it in GitHub Desktop.
Evaluate CSS text to add it to the page.
// Evaluate CSS text
// Can be used to add CSS to the page from XHR response, or localStorage, etc.
function evalCssText(text) {
// When Base64 encoding the data URI CSS urls, such as background images will not load if they aren't fully qualified URLs
// NOTE: Change this to whatever matches your path. All my images are loaded from a /static directory.
text = text.replace(/\/static\//gim, window.location.protocol + '//' + window.location.host + '/static/');
var href = 'data:text/css;base64,' + Base64.encode(text);
var node = new Element('link');
node.set('type', 'text/css');
node.set('rel', 'stylesheet');
node.set('href', href);
node.inject(document.head);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment