home / experts / javascript / column27 |
|
As explained earlier in this column, Netscape Navigator and Internet Explorer differ a lot in their document models and HTML implementations. If it's important for you to support both browsers, you need to handle these differences in your code somehow. There are several methods you can use for handling these differences. Your programming skills and experience will probably guide you as to which strategy to take at each particular case you tackle. A combination of all methods will usually work the best. The trivial way to handle browser inconsistencies is by conditional branching. One of the first subjects in every programming course is how to handle conditional branches, usually via the
Obviously, you can assume that if the browser is not Navigator 4.5, it is Internet Explorer 4.5, and you can omit the condition on the second line above. The variable A slight variation on the conditional branching is the conditional selector structure. This structure can be found in the C language as well. Here is the former example, converted to use the conditional selector:
This statement should read that the variable on the left hand side of the assignment is set to the expression following the A common trick for handling browser inconsistencies is to use the conditional branching at the property name level and have the assignments free from browser dependency. As explained earlier in this column, the B property assignment in Netscape Navigator looks like this:
while an equivalent assignment in Internet Explorer would look like this:
If you want to keep the same assignment in both browsers, you can build a single assignment:
where
Notice the Another method to deal with browser inconsistencies is what's sometimes called API (Application Program Interface). API (more often than not referred to as Application Procedural Interface) is probably the simplest tool communication protocol. When Program A needs to interface with Program B, it is best to build an API (Procedural Interface) for Program B such that Program A can call it in a well structured method. Program B's internals are hidden from Program A, and no global variables are allowed to be shared, thus resembling some object oriented principles. In case of JavaScript, this method boils down to providing a set of functions that deal with browser inconsistencies. The application that uses this function does not have to deal with these differences, as they all being taken care of by the functions. DOCJSLIB is a library of such functions and serves as an example for this method. We prefer to call this method Cross-Browser Library. DOCJSLIB is an implementation of such a method. |
Created: October 12, 1998
Revised: October 12, 1998
URL: http://www.webreference.com/js/column27/api.html