Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting IE8 reliably using javascript

I have written a web app that requires IE version 8 (or higher presumably). If I run IE8 on a clean windows install on a VM it reports 'MSIE 8.0' as a user agent when queried with navigator.userAgent in javascript. But on a colleagues windows 7 machine his IE reports version 8 in the Help|About window, but the user agent string is 'MSIE 7.0'.

I figure that somewhere on his machine there is a setting that's telling IE to spoof the previous version, some kind of compatibility setting I presume, but for the life of me I can't find it. I'm not setting up quirksmode or IE7 compatibility mode from my end.

like image 493
sirlark Avatar asked Aug 31 '10 13:08

sirlark


3 Answers

<meta http-equiv="X-UA-Compatible" content="IE=8">
<script type="text/javascript">
var is_ie8_or_newer = false;
</script>
<!--[if gte IE 8]>
<script type="text/javascript">
is_ie8_or_newer = true;
</script>
<![endif]-->
like image 163
Quentin Avatar answered Sep 20 '22 16:09

Quentin


The user agent is not a sensible or reliable way of determining the browser version. Why don't you look for the presence of the feature you require by making it IE8 only and use that? That is a much more reliable method.

like image 22
Woody Avatar answered Sep 18 '22 16:09

Woody


The most entertaining trick I've seen — without having any idea of how efficient it is — is to leverage the IE conditional comment feature dynamically. To do that, your code takes a hidden <div> or a <div> in a document fragment, or whatever, and inserts into it some HTML surrounded by a conditional comment coded to check for a specific browser version:

var dummy = document.getElementById('dummy');
dummy.innerHTML = '<!' + '--[if IE 8]>x<![endif]-->';
var isIE8 = dummy.innerHTML === 'x';

IE8 can show a little button next to the URL box that switches the browser between IE7 mode and IE8 mode. You can open up the "Developer Tools" and that'll tell you what the current setting is.

like image 33
Pointy Avatar answered Sep 20 '22 16:09

Pointy