Created
April 3, 2013 20:11
-
-
Save mzabriskie/5304777 to your computer and use it in GitHub Desktop.
Evaluate CSS text to add it to the page.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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