views:

97

answers:

4

I am working in an enterprise CMS (Autonomy/Interwoven Teamsite) that does not give me direct access to the head of a page. I can only link style sheets and add external js files. Normally I would add a conditional comment to link an ie6/ie7 stylesheet. In some searching I've found a way to target ie with conditional commenting inside js and specific ie versions based on the jscript version

in js:

/*@cc_on
    document.createStyleSheet("/css/all_ie_fixes.css");
    /*@if (@_jscript_version = 5.6)
        document.createStyleSheet("/css/ie_6.css");
    /*@end
@*/

This seems like an ugly hack. Any suggestions?

A: 

If you're doing anything at all with IE6, then your code will be full of ugly hacks. Future maintainers of your code will know this and sympathise, rather than curse your name. If it works, then go with it.

nickf
+1  A: 

Try Conditional CSS:

// Conditional block example  

[if IE] @import('ie.css');
Nimbuz
Thanks, Nimbuz - Conditional CSS looks nice ... but I don't think I could get approval for server-side installation.
Luke
+1  A: 

There's probably no non-ugly way to do this. That said, using the user-agent detection provided by a library like YUI (relevant YUI doc) will arguably result in slightly clearer and more explicit code than the above hack. Something like:

if (YAHOO.env.ua.ie >= 6 && YAHOO.env.ua.ie < 7)
{
        document.createStyleSheet("/css/ie_6.css");
}

Ugly, yes. But it's pretty clear what the intent is.

Ben
I like the explicitness of this, Ben - and will check out the YUI doc. Do you think it is worth the weight of adding the library if I only use YUI for this one fuction?
Luke
No, probably not -- but I believe jQuery has something similar (if you want to use other jQuery functionality). And there might be a good standalone version detecting library. (would really suggest this as opposed to trying to roll your own detection, which apparently gets nightmarish)
Ben
+1  A: 

It's frowned on by some people but there are css hacks that target IE, and don't require conditional comments.

For example only IE6 will read this style:

* html p {color:red;}

IE6 is too stupid to read this one:

html>body p {color:red;}

A quick google search turns up many others: http://www.webdevout.net/css-hacks#in%5Fcss-selectors

samg