If you want to use data URIs for your CSS content, make sure to properly declare them. A working example is shown below:
// Your CSS code goes here
var css = '*{color:red;}';
// Dynamically create a <link> element
var link = document.createElement('link');
link.rel = 'stylesheet';
link.href = 'data:text/css;charset=UTF-8,' + encodeURIComponent(css);
// Append the <link> to the <head>
document.getElementsByTagName('head')[0].appendChild(link);
This will result in the text on the page turning red.
If your CSS is lengthy, consider base64 encoding it. In this case, your .href
should be:
// Note: You'll need to use a base64_encode implementation for this,
// as JavaScript does not have one built-in.
link.href = 'data:text/css;base64,' + base64_encode(css);