Still kind of annoying that there doesn't seem to be a simpler purely client-side solution though.
There is. You totally misunderstood me though. Let me try to explain it a different way.
Let's say there are a bunch of divs on your page. We'll give them all a class of "gray". Add a <noscript> to test when JS is off. So your HTML looks like this:
HTML Code:
<noscript><h1>Javascript is off!</h1></noscript>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
With JavaScript off, we want a light gray background (#eee) and with JS on you want a dark background.
So, create two stylesheets: light.css and dark.css. Add the following styles:
light.css:
Code:
.gray{
background:#eee;
border:1px solid #ccc;
height:150px;
width:300px;
float:left;
margin:0 5px 5px 0;
display:block;
}
dark.css
Code:
.gray{
background:#888;
border-color:#000;
}
light.css is the default CSS file. So, include that in the <head> of the page with your regular <link> element. You can include it directly too.
Now, add the following JS to import dark.css with javascript:
Code:
<script type="text/javascript">
function includeCSS(p_file) {
var v_css = document.createElement('link');
v_css.rel = 'stylesheet'
v_css.type = 'text/css';
v_css.href = p_file;
document.getElementsByTagName('head')[0].appendChild(v_css);
}
includeCSS('dark.css');
</script>
This will import dark.css if javascript is enabled. It's important that this JS go below the link to light.css.
All together:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<style type="text/css">
.gray{
background:#eee;
border:1px solid #ccc;
height:150px;
width:300px;
float:left;
margin:0 5px 5px 0;
display:block;
}
</style>
<script type="text/javascript">
function includeCSS(p_file) {
var v_css = document.createElement('link');
v_css.rel = 'stylesheet'
v_css.type = 'text/css';
v_css.href = p_file;
document.getElementsByTagName('head')[0].appendChild(v_css);
}
includeCSS('dark.css');
</script>
</head>
<body>
<noscript><h1>Javascript is off!</h1></noscript>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
<div class="gray"> </div>
</body>
</html>
Bookmarks