« Overview | Part 2 »

Part 1 - Detecting the user’s browser

In order to detect the type of browser the user is using, we are going to use a method called object detection. It sounds scary but it’s very simple: different browsers have support for different Javascript objects. So all we have to do is try to access these objects and we’ll know which browser we’re dealing with.

Here is the Javascript code:

var firefox = document.getElementById && !document.all;


That’s all it takes!

Both Firefox and IE support the getElementById function (more about it in part 2), so we also have to make sure that document.all is null (as it’s an IE only object) to be sure that the user is using Firefox.

So simply put, if firefox is false the user is using IE (or some other browser, but we only care about IE and Firefox at the moment), otherwise the user is using Firefox.