PDA

View Full Version : Best way to detect document.compatMode in IE?


WA
11-26-2003, 01:21 AM
Hi:
Just wondering, how do you guys detect the setting for document.compatMode in IE to account for the doctype setting in your scripts? Previously I used the code:

function ietruebody(){
return (document.compatMode!="BackCompat")? document.documentElement : document.body
}
//example:
var x=ietruebody().scrollLeft


though just realized this in fact creates problems in IE5.5 and below (since the "compatMode" property doesn't exist in those browsers). I've since figured something like:

function ietruebody(){
return (document.compatMode &&
document.compatMode!="BackCompat")? document.documentElement : document.body
}
//example:
var x=ietruebody().scrollLeft


which should degrade well, though I haven't tested it yet.

Is there any better way to accomplish the above? I was thinking of directly testing for:

document.compatMode=="CSS1Compat"

though am not sure whether "CSS1Compat" might change in IE7, so I thought I'd simply test for "!=BackCompat" instead.

Thanks,

liorean
11-26-2003, 01:43 AM
Currently, I would use 'CSS1Compat' since 'BackCompat' is used by moz and iew, but op7 uses 'QuirksMode' instead, and IIRC saf/konq doesn't specify it at all.

WA
11-26-2003, 03:34 AM
Thanks liorean. I guess I'm just afraid 'CSS1Compat' might be changed to 'CSS2Compat' or something when IE7 is released.

liorean
11-26-2003, 11:26 AM
Then, /CSS/.test(document.compatMode) might be better, I would think.

brothercake
11-26-2003, 02:55 PM
I do it like this; far more long winded, but reliable, and gives you a set of IE variables:

//browser object
var browser = new Object;

//get UA string
browser.ua = navigator.userAgent.toLowerCase();

//internet explorer
browser.ie = (
typeof document.getElementById!="undefined"
&&
typeof document.all!="undefined"
&&
typeof window.opera=="undefined"
);

//ie5
browser.ie5 = (browser.ie && browser.ua.indexOf("msie 5")!=-1);

//ie5.5
browser.ie55 = (browser.ie && browser.ua.indexOf("msie 5.5")!=-1);

//ie5.0
browser.ie50 = (browser.ie5 && !browser.ie55);

//ie6
browser.ie6 = (browser.ie && browser.ua.indexOf("msie 6")!=-1);

//quirks mode
browser.quirks = (browser.ie5 || (browser.ie6 && document.compatMode == 'BackCompat'));

Incidentally - I don't think there's going to be an IE7 in the foreseeable future, if ever; Longhorn is still two years away and that's currently shipping with IE6.1

liorean
11-26-2003, 03:06 PM
Originally posted by brothercake
Incidentally - I don't think there's going to be an IE7 in the foreseeable future, if ever; Longhorn is still two years away and that's currently shipping with IE6.1 Unless they have changed the version since PDC, they are still running 6.05, actually. The only addition that seems worth to mention is built-in popup blocking.

brothercake
11-26-2003, 03:18 PM
6.05 is it? I was told 6.1 - but yeah, popup blocking as the only difference.

I think we have to accept the fact that MS gives less than flying fark about IE and web standards - they don't see any need to update it.

[Sorry .. OT ..]

WA
11-27-2003, 07:53 PM
Good code brothercake, thanks. It makes me wonder why I'm testing document.compatMode via a function- waste of resources.