2

I have some JavaScript functions that include code like:

if (document.all)
{ document.getElementById('subrow').style.display = 'block'; }
else
{ document.getElementById('subrow').style.display = 'table-row'; }

IE 11 returns false for document.all so the above code no longer works, but I need it to work for earlier versions of IE as well as IE 11 and other browsers.

How can I achieve that? Earlier versions of IE do not understand table-row so I have to be able to differentiate.

Jordan Gray
  • 16,306
  • 3
  • 53
  • 69
Martin Smellworse
  • 1,702
  • 3
  • 28
  • 46

3 Answers3

1

Don't—use feature detection.

Trying to detect browsers with indirect tests like checking for document.all is, as you have discovered, very unreliable. Best practice is to use feature detection, not browser detection.

Instead, if you want to know if table-row is supported, you can check for that directly. This makes your code far more portable because it doesn't tie it to particular versions. Here's one way you could do this based on the Modernizr display test for table layout:

function isTableRowSupported() {
    try {
        var style = document.createElement("div").style;
        style.display = 'table-row';
        return style.display == 'table-row';
    } catch (e) {
        return false;
    }
}

jsFiddle demo

Jordan Gray
  • 16,306
  • 3
  • 53
  • 69
0

You can use and check "ActiveXObject" in window instead which will apply to all IE versions.

document.all was(!) a quick check to see if it's IE.

so :

if ("ActiveXObject" in window)
{ document.getElementById('subrow').style.display = 'block'; }
else
{ document.getElementById('subrow').style.display = 'table-row'; }
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

Instead of document.all, if you want to do a check for IE then do it like this:

if(/msie|trident/i.test(navigator.userAgent)){
   document.getElementById('subrow').style.display = 'block';
} else {
   document.getElementById('subrow').style.display = 'table-row';
}

Note: Trident is the rendering engine of IE, which is available in all the versions of IE.

Ashish Kumar
  • 2,991
  • 3
  • 18
  • 27