How to create a CSS link element using JavaScript


As you might know, some of the CSS code is no W3C valid. Even though Google has HTML and CSS errors in it’s home page it doesn’t mean you want them in yours.
If you want the green sticker for your site you can load this invalid CSS in an other file. Then, load this file using JavaScript.
Here’s how to do it:
<script>   
var cssNode = document.createElement('link');
cssNode.type = 'text/css';
cssNode.rel = 'stylesheet';
cssNode.href = '/css/invalid.css';
cssNode.media = 'screen';
document.getElementsByTagName("head")[0].appendChild(cssNode);
</script>
How to create a CSS link element using JavaScript How to create a CSS link element using JavaScript Reviewed by JohnBlogger on 3:01 PM Rating: 5

No comments: