Boder
07-08-2004, 04:27 AM
Hello, new user here!
I have been getting into JavaScript, and at this point it seems basically everything useful you do with it uses the DOM, so I figure I'd post here.
I am changing the background color of my page. I have found a few ways to do it but I'm wondering which is the "right" way. My page is Strict XHTML so I don't really want to break that.
document.bgColor = 'red';
doesn't work with a css stylesheet defined like so...
body { background-color: black; }
It seems necessary to change the background when stylesheets are applied by actually changing the stylesheet instead of the document itself:
var theRules = new Array();
if (document.styleSheets[0].cssRules)
theRules = document.styleSheets[0].cssRules
else if (document.styleSheets[0].rules)
theRules = document.styleSheets[0].rules
theRules[0].style.backgroundColor = 'red';
i can use the ( document.bgColor = 'red'; ) if I use the depreciated attribute 'bgcolor' of the 'body' tag. But I do not want to this.
The last option available is to do neither and simply run a script that chooses the background color when the page loads.
Which brings me to another question :thumbsup:
How do you run scripts when a page loads? Right now I just run the appropriate script functions after all the HTML content and before the ending </body> tag, because many functions use the DOM and reference certain tags. Is this fine?
Thanks to anyone with helpful words.
I have been getting into JavaScript, and at this point it seems basically everything useful you do with it uses the DOM, so I figure I'd post here.
I am changing the background color of my page. I have found a few ways to do it but I'm wondering which is the "right" way. My page is Strict XHTML so I don't really want to break that.
document.bgColor = 'red';
doesn't work with a css stylesheet defined like so...
body { background-color: black; }
It seems necessary to change the background when stylesheets are applied by actually changing the stylesheet instead of the document itself:
var theRules = new Array();
if (document.styleSheets[0].cssRules)
theRules = document.styleSheets[0].cssRules
else if (document.styleSheets[0].rules)
theRules = document.styleSheets[0].rules
theRules[0].style.backgroundColor = 'red';
i can use the ( document.bgColor = 'red'; ) if I use the depreciated attribute 'bgcolor' of the 'body' tag. But I do not want to this.
The last option available is to do neither and simply run a script that chooses the background color when the page loads.
Which brings me to another question :thumbsup:
How do you run scripts when a page loads? Right now I just run the appropriate script functions after all the HTML content and before the ending </body> tag, because many functions use the DOM and reference certain tags. Is this fine?
Thanks to anyone with helpful words.