1. Home
  2. Computing & Technology
  3. JavaScript

Five Most Common Coding Errors
Browser Detection

By , About.com Guide

The third worst mistake that Javascript beginners make is to use Browser Detection to determine what Javascript code to run on which browser. The problem with doing this is that there are hundreds of different browsers that you would really need to test for in order to get the code exactly right with new versions being introduced just about every day (have you updated your browser detection script to recognise today's new browser versions?)

When people do use such a script they usually just test for the two or three most commonly used browsers and totally stuff up the web page for anyone using a less common browser. Even with the browsers that they do cater for a lot of work is required to recode the script every time a new version of one of those browsers comes out with different functionality.

A much better solution to using browser detection is to use Feature Sensing to test if the browser supports the particular code that you want to use.

About the only exception I can think of where it makes sense to use browser sensing instead of feature sensing is if you have a script that is written in JScript instead of Javascript. Since JScript only runs on Internet Explorer it would make sense to have the entire script automatically commented out if you have Identified Internet Explorer is not the browser being used. The reverse situation doesn't really apply since valid Javascript code is almost always valid JScript code as well.

Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript
  4. Javascript Tutorials
  5. Browser Detection>

©2009 About.com, a part of The New York Times Company.

All rights reserved.