View Single Post
Old 08-25-2005, 04:27 PM   #8
georgec
Registered User
 
Join Date: Apr 2001
Posts: 519
Default

Hi:
Assuming the CSS file:
Code:
body{
background-color: BLACK;
color:WHITE;
**

div.container{
position:absolute;
top:100;
left:100;
width:300;
height:300;
overflow:hidden;
background-color:RED;
color:GREEN; 
**
To read the background-color attribute using JavaScript, you would do:
Code:
<script type="text/javascript">
var crossrule=document.styleSheets[0].cssRules? document.styleSheets[0].cssRules : document.styleSheets[0].rules
bgattribute=crossrule[0].style.backgroundColor //returns "black"
</script>

The above script assumes the CSS file is the very first stylesheet on your page (hence document.styleSheets[0]).

To answer your other question regarding writing to files, unfortunately JavaScript can't do that. You'll need something like PHP in conjunction to actually rewrite the CSS file. However, I doubt that's what you really want, as it means every time some user customizes the CSS file, it affects everyone else viewing your site. A better way may be to use cookies, so user changes are stored inside his/her computer (ie: to the backgroundColor attribute), then retrieved by your script to tailor the background color to his choice.
georgec is offline   Reply With Quote