Please send questions to
st10@humboldt.edu .
Random projections, Session 8, 9-14-06
* since JavaScript is not implemented with the same features in
different browsers,
it is useful for a page to be able to see what kind of browser
it is dealing with, and to act accordingly
* The navigator object created for each page when it is loaded
into a browser has properties containing such information.
(for example, see http://www.w3schools.com/htmldom/dom_obj_navigator.asp)
* appName - name of the browser
* appVersion - version of that browser (and more!)
* js_nav1.html: demos these, and a little table HTML
(google "Table Sampler" for more table HTML examples...)
* A few approaches for dealing with browser incompatibilities
in JavaScript..
* (Ch. 20, JavaScript: the Definitive Guide, 4th edition)
* (keep in mind - some of these are more advisable than
others, depending on YOUR situation)
* least-common-denominator approach - just avoid using
features known to have incompatibility issues
* could just NOT use such features;
* could set up your own properties for non-standard
properties...
myObj.newProp = 27;
* defensive coding - you write JavaScript that contains
platform-independent workarounds for platform-specific
incompatibilities
* feature testing - before you use a feature, you test for
it, so if it isn't there you can either NOT do it or
provide a workaround
* you can put an object property as the condition for
an if statement -- if it exists, it'll be treated as
a true condition, and if not, it'll be treated as a
false condition
if ( ! myObj.desiredProperty )
{
myObj.desiredProperty = something
(or otherwise handle)
}
... myObj.desiredProperty ...
* how can you see if a method doesn't exist?
...type its name WITHOUT () or arguments!
System will check if it exists WITHOUT invoking it!
if (obj.desiredMethod)
{
val = obj.desiredMethod(whatever)
}
else
{
val = obj.alternativeMethod(whatever)
}
* Platform-specific workarounds
* what if you can't test easily for some issue on a particular
platform? You might just see if you're on that platform,
and say what you do in that case.
* Ignore the problem
* Fail Gracefully
* means recgnizing that the required features are not
available and informing the user that he/she will not
be able to use your JavaScript program