Today I was updating some JavaScript code to support the rapidly-approaching Internet Explorer 7. there were a few places in the Code where there were ie-specific workarounds, which happily are no longer needed in IE 7
Thanks to its improved standards support. Yay position: fixed!
Where the code used to check for if (IE ){...}, now I wanted it to check for if (ie6orlower ){...}. so how to you tell the difference between IE 6 and IE 7 +? You cocould parse the User-Agent string, but I 'd rather detect changes in the JavaScript Object Model. Here's what I came up:
If (typeof document. Body. style. maxheight! = "Undefined "){
// IE 7, Mozilla, Safari, opera 9
} Else {
// IE6, older browsers
}
This distinguishes between browsers based on the fact that IE 7 knows about the maxheight CSS property, whereas previous versions of IE didn't. Does that seem like a sane approach to you?
Update: over at ajaxian Arjan points out that it's a bit simpler to check for window. XMLHttpRequest, which is also new in IE 7.
Javascript:
-
-
- If (TypeofDocument.Body.Style.Maxheight! ="Undefined") {
-
- // IE 7, Mozilla, Safari, opera 9
-
- } Else {
- // IE6, older browsers
-
- }
You can also use the xhr check:
-
-
If (window. XMLHttpRequest) {
// IE 7, Mozilla, Safari, opera 9
}else {
// IE6, older browsers
}
if script is executed width, then document. body is not available yet.
If (window. XMLHttpRequest) {
If (document. epando) {
alert ("IE7");
// IE7
}else {
// Mozilla, Safari, opera 9... ETC
alert ("Mozilla");
}< BR >}else {
// IE6, older browsers
alert ("IE6 ");
}< br>
I tested it and found that the last method is different from IE7 or Firefox. Please kindly advise!