views:

349

answers:

7

If I were to remove my CSS stylesheet and set all the properties via a JavaScript file, would that be possible? That is, would there be anything I couldn't access via the JS DOM API?

A: 

I'm not sure if you can set different media types like "print" from in javascript.

[edit]
Actually, I take that back. You might be able to use cssText to do it.

Joel Coehoorn
+1  A: 

The only ones I can think of are possibly IE CSS expressions (such as ones commonly used to fix PNGs in IE6).

However I don't think this is generally a good idea, as you want to keep your content (HTML), layout (CSS) and logic (javascript) seperate for maintainability reasons. Not to mention if people visit your site with javascript disabled.

tj111
ehh, i disagree on this one
Shawn Simon
i would think if the browsers supports it, it is accessible, it's all about the DOM baby! The DOM IS your page, it's what the browser manipulate in memory and renders to your screen...
Sander Versluys
I agree with this point. You should be setting CSS classes in JavaScript as much as possible (except for things like animation, where you need to adjust specific properties). Less code in JavaScript, cleaner separation and easier to find all styles.
Ryan Doherty
+4  A: 

Everything in CSS can be accessed through JS, but bear in mind that users with JS disabled won't get any style at all!

  • CSS is for styling
  • JavaScript is for changing functionality

Don't merge them unless strictly necessary.

Seb
Yeah but I'm thinking of merging them for purely exploratory purposes. I'm not trying to propose that we all define our stylesheets in JS.
cdmckay
That's fine, just a suggestion ;)
Seb
A: 

A lots of interesting information about the DOM to CSS interface can be read from the W3C Document Object Model CSS2 Specification. For CSS3, they are still working on it.

So it is possible to manipulate any css property through CSS DOM stylesheets but it depends on what the browser supports.

Sander Versluys
A: 

I'm pretty sure that having the browser execute all that JS just to set a style would be much slower than applying a stylesheet, and possibly be more prone to cross browser problems.

Alex JL
A: 

Great question: "would there be anything I couldn't access via the JS DOM API?"

I think the answer is yes. See below:

IE8 now wants this for the opacity property: -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=50)"

so does anyone know how to change this property dynamically with Javascript DOM access (DHTML)?

Point taken?

I thought there was a filter property that could be set - or at the very least added on via JS
Hugoware
A: 

try msFilter