Reputation: 1762
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.
Upvotes: 2
Views: 403
Reputation: 16499
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;
}
}
Upvotes: 1
Reputation: 3039
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.
Upvotes: 0
Reputation: 148694
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'; }
Upvotes: 0