I\'ve seen many examples of loading CSS and javascript dynamically from a file. Here\'s a good example. But is there a way to load CSS or javascript as a string? For exam
Another way without using potentially dangerous eval
and innerHTML
is creating a link
element with base64 encoded URL:
function loadCSS(cssStyles) {
const link = document.createElement('link');
link.href = `data:text/css;base64,${btoa(cssStyles)}`;
link.type = 'text/css';
link.rel = 'stylesheet';
document.getElementsByTagName('head')[0].appendChild(link);
}