Saturday, November 24, 2007

Easiest way to check IE version with JavaScript

Alright, so I needed a easy way to execute custom JavaScript for clients using versions of IE lower than 7. All the scripts Google showed me were really huge and quite outdated. So I took what I found, and turned it into something really lightweight. Note that this is just a quick hack, but so far it seems to be enough for my needs.


var Browser = {
Version: function() {
var version = 999; // we assume a sane browser
if (navigator.appVersion.indexOf("MSIE") != -1)
// bah, IE again, lets downgrade version number
version = parseFloat(navigator.appVersion.split("MSIE")[1]);
return version;

Which enables me to do stuff like this:

if (Browser.Version() < 7) {
... // if client is using IE6 or lower, run this code

..or this:

if (Browser.Version() >= 7) {
... // if client is using IE7 or a sane browser, run this code

It will simply return 999 if the browser is non-IE. Is it safe to assume that navigator.appVersion always will contain "MSIE" in uppercase?

Thursday, November 15, 2007

Quote of the year

"I get a little paranoid when stuff Just Works though so it's a good thing 99.9% of our customers run Internet Explorer. I'm pretty hard on IE but I will admit to at least one thing it does really well: destroying any sense of pleasantness a developer may feel when building web applications."

So true. Taken from this post.