javascript create css stylesheet code example
Example 1: link stylesheet in javascript
var cssId = 'myCss';
if (!document.getElementById(cssId))
{
var head = document.getElementsByTagName('head')[0];
var link = document.createElement('link');
link.id = cssId;
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'http://website.com/css/stylesheet.css';
link.media = 'all';
head.appendChild(link);
}
Example 2: add css style sheet with javascript
<head>
<script>
function myFunction() {
var x = document.createElement("LINK");
x.setAttribute("rel", "stylesheet");
x.setAttribute("type", "text/css");
x.setAttribute("href", "styles.css");
document.head.appendChild(x);
}
myFunction()
</script>
</head>