Jimnbigd said:
What is the recommended code to test browser type, for conditional
processing in Javascript?
There are no recommended tests for browser type or version. Indeed the
recommendation is not to care about the browser type or version at all,
because it is impossible to acquire that information.
Conditional execution tests are determined by reason for making the
execution conditional. So, for example, if one branch in code is going
to use a set of features that are only supported by a sub-set of
browsers then the test that controls the branching should be verifying
the existence of those features in the current execution environment
(and possible aspects of the behaviour of those features). So, for
example, given a desire to read the scroll offsets of a document, one
group of browsers will make that information available as global
pageXOffset
and pageYOffset properties, which would be numeric where implemented and
undefined otherwise. The test would be:-
if('number' == pageXOffset){
...// code that reads global pageX/YOffset
}
- and the block of the if statement would never be executed in an
environment that does not implant the specific feature of interest.
And - else - branches with other conditional tests could test for
alternative features and conditions that apply to other environments,
again testing for the features/conditions that represent the reason for
the existence of the branches.
I have seen tests for "document.all".
With the exception of code that tests for - document.all - as a
condition that has to be satisfied prior to the use of -
document.all -(only), that is termed "object inference"; The inferring
of all the features of an object model form a test on one particular
feature. Object inference is an invalid strategy because it is based on
invalid assumptions (that are more often than not actually untrue in
reality). The inference that a browser is Microsoft IE based on the
environment implementing - document.all - is particularly bogus.
I have seen tests for the actual browser name, or
substrings in the browser name.
There is no available DOM property that represents the browser name. The
nearest being the much spoofed - navigator.appName -. Usually these
tests are based on the navigator.usetAgent - property, which reflects
the HTTP User-Agent header being used by the browser. HTTP 1.1
User-Agent headers are not specified as providing any information about
the client. And in reality User-Agent headers of different browsers are
often indistinguishable; they cannot be examine to provide
discriminating information, so the User-Agent header cannot be used to
identify the browser type or version.
What do you all recommend?
Feature detection, as no browser detection strategy works and in the
vast majority of cases feature detection does. Making Feature detection
a far superior strategy.
Richard.