Can someone tell me how to do it so that only browsers with javascript get the css file. I want to put it in a js file as it will make managing it easier. So in other words, don't provide alternatives![]()
| SitePoint Sponsor |




Can someone tell me how to do it so that only browsers with javascript get the css file. I want to put it in a js file as it will make managing it easier. So in other words, don't provide alternatives![]()





You can lazy load a CSS file with javascript.
For example:
Code:window.onload = function() { var newCSS = document.createElement('link'); newCSS.href = 'css/for-javascript-users.css'; //path to CSS file newCSS.type = 'text/css'; newCSS.rel = 'stylesheet'; //Add CSS to Document var head = document.getElementsByTagName('head')[0]; if(!head) return; head.appendChild(newCSS); }
If you are not using an XHTML doctype then you can do this:
I've used this alot and I see nothing wrong with using this method for HTML doctypes - it is as cross-browser as you can get. But you can't use this method in XHTML... but then, most of the people who use an XHTML doctype are not really serving XHTMLCode:document.write("<style type='text/css'>.myStyleOverrides {...}<\/style");![]()
Cross-Browser.com, Home of the X Library
Bookmarks