Menu
Home
Articles
Flash
Clipart
E-books
Contact Us
Site Map
Home arrow Articles arrow Tips and Tricks arrow JavaScript - How to determine the browser type?
JavaScript - How to determine the browser type? E-mail
Tuesday, 19 July 2005
The reason why JavaScript code should be aware about the browser which is used by the surfer is obvious - browsers are different in many ways, including the architecture and execution of the JavaScript code itself.The reason why JavaScript code should be aware about the browser which is used by the surfer is obvious - browsers are different in many ways, including the architecture and execution of the JavaScript code itself. Currently there is over 100 software products listed at download.com in category Windows, Internet, Browsers, Browsers. The one that tries to complete the task of recognizing all this stuff from JavaScript and providing compatibility to all of them will end up like the man wo tried to calculate the "PI" number - his grave will have a long incomplete to-do list, or he will loose his mind. In the best case scenario, that person will blame computers, internet, modern technologies and become a monk somewhere in middle Asia. However, commercial JavaScript developers focus on support for major commercial browsers: Internet Explorer, Netscape Navigator and Opera. Keeping in mind that all smaller browsers are trying to provide compatibility with at least one popular browser. Relevant support and compatibility with mentioned trinity will most likely provide good output with any other browser as well. In any case its the best ratio between development time and compatibility, which has been proved by the market for many years. There is kind of an unwanted child in the mentioned family - its the old Netscape version 4.xx. A lot of commercial JavaScript applications still support it, but NE 4.xx so different from the rest of the modern browsers and creates a real headache for everyone who provides compatibility to it. Still, its not just a stylish thing to express idea an idea like "we are so professional that our JS code supports any possible browser, even the really old ones like NE 4.xx". The reason is the Mac platform. The old Mac OS is still using NE 4.xx and unlike Windows 95 which is rarely used nowadays, these old Mac OS users are still a considerably big part of the Mac audience. So its smart not to ignore them if possible and it's a really good business policy to provide at least a simplified version of the code if an old Netscape is detected. So now, when things are clarified and milestones are set, let the magic begin! The technique described below will give you everything you need to find out the browser type and take relevant actions from withing your JS code:
Copy and paste this code in a new html file, open it with your favorite browser and you will instantly get an idea of which browser platform it presents. Detecting Opera is not mandatory, as Opera tries to be compatible with Internet Explorer JavaScript code as much as possible. Still, its handy to have an option to recognize it. Other flags: document.layers and document.all not only refer to the mentioned browsers but also may show the way to provide compatibility. If the browser supports layers then its most likely that it will work in same way as the old Netscape. If document.all is set then the browser will probably have the best compatibility with Internet Explorer JavaScript format. So this kind of "page-features" recognition works much better than doing a simple scan of the browser name and searching for browser names inside it.
Last Updated ( Tuesday, 19 July 2005 )
< Prev