Reply
Can you change CSS with javascript?
Old 02-25-2007, 02:04 AM Can you change CSS with javascript?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
I really don't want to have one style sheet for IE 6 and another for well-behaved browsers. I used a pretty common hack to get most of my png files with any amount of alpha to work, but one of them is a background image in a rule laid out in my CSS file.

I think I can get it to display properly if I can change it from being a background-image to filterrogid:XImageTransform.Microsoft.AlphaImage Loader, but I'm not really sure how to do this, or whether it's possible? The div has an ID, and the style is applied that way ( #leftNAV in CSS ), so I can grab it with document.getElementById, but I don't know if you're allowed to change it, or what property to use.

And being stumped like this makes me wonder if I'm approaching this the wrong way. I can't ignore how many people still use IE 666, but I hate the idea of making my site more and more complex with all kinds of hacks, especially for an outdated browser.

Would it make sense to recommend and link to FireFox? A long time ago I read "this site best viewed in Netscape" offended people with other browsers; I know times have changed, but any thoughts on whether this is a good idea?
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
When You Register, These Ads Go Away!
Old 02-25-2007, 02:38 AM Re: Can you change CSS with javascript?
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,945
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Saying "this site is viewed in" is never a good idea. You're forcing your rules on someone else, and that's not a good practice.

Personally, I'd continue to do what you were doing. If you can get it to work the way you want it to properly across all browsers and the code is relatively clean (it'll never be completely clean as you'll have the hack no matter what), then there really is no wrong answer.
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Old 02-25-2007, 04:03 AM Re: Can you change CSS with javascript?
chrishirst's Avatar
Super Moderator

Posts: 13,590
Location: Blackpool. UK
To change a hyphenated CSS property with javascript you use the property name change the first letter of the second word to Uppercase and remove the hyphen
So:
background-image = backgroundImage
margin-top = marginTop
and so on

to reference a CSS property use the style object

document.getElementById("elementID").style.propert yName = PropertyValue

You don't really need to use "hacks" as such, which may have unwanted effects, Just use IE conditional comments
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-25-2007, 04:39 AM Re: Can you change CSS with javascript?
Experienced Talker

Posts: 30
Name: Ben Henick
To add more detail to the JavaScript answer... Every DOM node (element) has a .style property, which in turn has a collection of properties for every CSS attribute recognized by the browser. chrishirst already described the camelCasing of those properties.

So for example, you might wind up with:

document.getElementById("main").getElementsByTagNa me("a")[foo].textDecoration = "none";

MSDN Library's Web docs are really helpful in this respect.

As for IE... what I do is to put all of the IE-focussed rules in their own linked stylesheet, and the non-IE7 rules live behind

* html etc.

within that stylesheet.

If you feel that scripting styles would be a better approach in the long run, just make sure you can live with the potential range of consequences that users will need to deal with as a result (specifically in regard to resource usage and site accessibility).

Finally, I don't see what's wrong with putting a "Best Viewed with" notice in the footer of a hobby/personal site. It definitely brings back some unpleasant memories, but it's the equivalent of pointing out to your users that some contents may have settled during shipping.

If you're getting paid, it's a different story - the time spent testing and doing QA against IE6 should be accounted for in your Statement of Work and/or contract, and properly invoiced. If the client doesn't like it, then they need to be briefed on the consequences.
persist1 is offline
Reply With Quote
View Public Profile
 
Old 02-25-2007, 08:13 AM Re: Can you change CSS with javascript?
Experienced Talker

Posts: 35
ISDN is crap for web developing...google it...
__________________
LIKE MY POST? PLEASE ADD TO MY TALKUPATION, ON THE LEFT.
My MSN: existenz.pb@gmail.com
eXistenZ is offline
Reply With Quote
View Public Profile
 
Old 02-25-2007, 09:46 AM Re: Can you change CSS with javascript?
chrishirst's Avatar
Super Moderator

Posts: 13,590
Location: Blackpool. UK
Quote:
Originally Posted by eXistenZ View Post
ISDN is crap for web developing...google it...
And what exactly has a digital telecomms syatem got to do with web developing ?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-25-2007, 10:10 AM Re: Can you change CSS with javascript?
LadynRed's Avatar
Super Moderator

Posts: 6,554
Location: Tennessee
There is nothing whatsoever wrong with using a separate stylesheet for IE versions - that's what conditional comments were meant to do - steer IE to use a separate stylesheet. However, you do not have to reproduce an ENTIRE stylesheet for IE, the only things you put in the 'fixes' CSS file is the hacks we are forced to use to kick IE into submission.
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!
"Using or working with IE is like having to wear a 1970's polyester suit with pantyhose and a girdle, to work everyday"
Carolina Corvette Club
LadynRed is online now
Reply With Quote
View Public Profile
 
Old 02-25-2007, 04:18 PM Re: Can you change CSS with javascript?
ForrestCroce's Avatar
Half Man, Half Amazing

Posts: 3,024
Name: Forrest Croce
Location: Seattle, WA
Well, I got more and better advice than I expected - thanks, everyone!

So it sounds like using javascript to change a property that was set with an external stylesheet is perfectly valid? I don't know why, but that threw me off. Can I set the backgroundImage to null?

If I went with a second stylesheet, how would that work? If I used a conditional comment to call it out ( and I'm already using one for pngfix ), that comes after the main stylesheet, will the IE 6 styles override the main ones? With FireFox and IE 7, I would want to set the background image to a png file, but with IE 6, I need to not set the background image, and instead set the "filter" IE-only ... well, whatever you would call it. From what I gather it's a set of ActiveX extensions built into the browser, and won't run on server operating systems. But there's nothing I can really do about that.
ForrestCroce is offline
Reply With Quote
View Public Profile Visit ForrestCroce's homepage!
 
Old 02-26-2007, 02:54 PM Re: Can you change CSS with javascript?
LadynRed's Avatar
Super Moderator

Posts: 6,554
Location: Tennessee
If you use conditional comments to target IE 6 and below, it WILL look to your main CSS file for everything else, but for those 'fixes' in the separate CSS file, it will use THOSE instead. That is why it isn't necessary to completely duplicate a CSS file, it looks at ONLY the rules you give it for IE.

There is a javascript method for PNG transparency that works pretty well, as long as you don't want any repeating:
http://homepage.ntlworld.com/bobosola/pnghowto.htm
__________________
Web Goddess & Web Standards Evangelist :) - Tables Be Gone !!
"Using or working with IE is like having to wear a 1970's polyester suit with pantyhose and a girdle, to work everyday"
Carolina Corvette Club
LadynRed is online now
Reply With Quote
View Public Profile
 
Old 02-27-2007, 03:05 AM Re: Can you change CSS with javascript?
chrishirst's Avatar
Super Moderator

Posts: 13,590
Location: Blackpool. UK
Quote:
Can I set the backgroundImage to null
Not to null, just set to to an empty string.

.backgroundImage = '';
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 03-08-2007, 02:19 PM Re: Can you change CSS with javascript?
Novice Talker

Posts: 5
Name: Moses
http://www.w3schools.com/htmldom/default.asp
mosesdinakaran is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Can you change CSS with javascript?
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.18992 seconds with 12 queries