53Chapter 13 .JavaScript Essentials the error, you can (My space web page)
53Chapter 13 .JavaScript Essentials the error, you can cascade the conditional tests with the help of the && operator. The following fragment tests for the existence of both the document.bodyobject and the document.body.styleproperty: if (document.body && document.body.style) { // statements that work on the body s style property } If the test for document.body fails, JavaScript bypasses the second test. One potential gotcha to using conditional expressions to test for the existence of an object s property is that even if the property exists but its value is zero or an empty string, the conditional test reports that the property does not exist. To workaround this potential problem, the conditional expression can examine the data type of the value to ensure that the property genuinely exists. A non-existent property for an object reports a data type of undefined. Use the typeofoperator (Chapter 40) to test for a valid property: if (document.body && typeof document.body.scroll != undefined ) { // statements that work on the body s scroll property } Object detection is the wave of the future, and I wholeheartedly recommend designing your scripts to take advantage of it in lieu of branching on particular browser name strings and version numbers. Scriptable features are gradually find ing their way into browsers embedded in a wide range of non-traditional computing devices. These browsers may not go by the same names and numbering systems that we know today, yet such browsers may be able to interpret your scripts. By testing for browser functionality, your scripts will likely require less maintenance in the future. You can see more object detection at work in Chapters 47 and 56. Designing for Compatibility Each new major release of a browser brings compatibility problems for page authors. It s not so much that old scripts break in the new versions (well-written scripts rarely break in new versions with the rare exception of the jump from NN4 to NN6). No, the problems center on the new features that attract designers when the designers forget to accommodate visitors who have not advanced to the latest and greatest browser version yet or who don t share your browser brand preference. Adding to these problems are numerous bugs, particularly in first-generation browsers from both Netscape and Microsoft. Worse still, some of these bugs affect only one operating system platform among the many supported by the browser. Even if you have access to all the browsers for testing, the process of finding the errors, tracking down the bugs, and implementing workarounds that won t break later browsers can be quite frustrating even when you ve scripted pages from the earliest days and have a long memory for ancient bug reports. Catering only to the lowest common denominator can more than double your development time due to the expanded testing matrix necessary to ensure a good working page in all operating systems and on all versions. Decide how important the scripted functionality you employ in a page is for every user. If you want some functionality that works only in a later browser, then you may have to be a bit auto cratic in defining the minimum browser for scripted access to your page any lesser browser gets shunted to a simpler presentation of your site s data.
Please visit our professional web hosting services to find out about cheap and reliable webhost service that will surely answer all your demands.