Checking Browser Capabilities


if (document.getElementById) {   // ... 

As you could see from the previous examples, relying on browser version numbers is not only difficult, but also not future-proof. A much better way to check whether a browser supports the features your application requires is to specifically check for the support of the special objects.

For instance, to use DOM (see Chapter 5, "DOM and DHTML"), you might want to try the preceding code. If the getElementById() method is implemented, document.getElementById (without parentheses) returns a reference to the function. If used within a condition, this evaluates to TRue. Therefore, the associated code is executed.

Another example: Internet Explorer supports ActiveX objects for certain applications, for instance XML support. However, only the Windows versions of IE know ActiveXthe Mac versions don't. So specifically checking for Internet Explorer creates problems for Mac users. If you specifically check for the ActiveX support, you avoid these issues:

if (window.ActiveXObject) {   // ... } 





JavaScript Phrasebook(c) Essential Code and Commands
JavaScript Phrasebook
ISBN: 0672328801
EAN: 2147483647
Year: 2006
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net