154Part IIIDocument Objects ReferenceObject detection works (Web server hosting) best when

154Part IIIDocument Objects ReferenceObject detection works best when you know for sure how all browsers implement the object. Inthe case of document.images, the implementation across browsers is identical, so it is a verysafe branching condition. That s not always the case, and you should use this feature with care- ful thought. For example, IE4 introduced a documentobject array called document.all, whichis used very frequently in building references to HTML element objects. NN4, however, did notimplement that array, but instead had a document-level array object called layers, which wasnot implemented in IE4. Unfortunately, many scripters used the existence of these array objectsas determinants for browser version. They set global variables signifying a minimum version ofIE4 if document.allexisted and NN4 if document.layersexisted. This is most dangerousbecause there is no way of knowing if a future version of a browser may adopt the object of theother browser brand or eliminate a language feature. For example, Opera in its native settingsupports the document.allarray. But if you expect that browser to support every detail of theIE4 browser, scripts will break left and right. This is why I recommend object detection not for browser version sniffing but for object avail- ability branching, as shown previously for images. Moreover, it is safest to implement objectdetection only when all major browser brands (and the W3C DOM recommendation) haveadopted the object so that behavior is predictable wherever your page loads in the future. Techniques for object detection include testing for the availability of an object s method. Areference to an object s method returns a value, so such a reference can be used in a condi- tional statement. For example, the following code fragment demonstrates how a function canreceive an argument containing the string ID of an element and convert the string to a validobject reference for three different document object models: function myFunc(elemID) { var obj; if (document.getElementById) { obj = document.getElementById(elemID); } else if (document.all) { obj = document.all(elemID); } else if (document.layers) { obj = document.layers[elemID]; } if (obj) { // statements that work on the object} } With this object detection scheme, it no longer matters which browser brand, operating sys- tem, and version supports a particular way of changing an element ID to an object reference. Whichever of the three documentobject properties or method is supported by the browser(or the first one, if the browser supports more than one), that is the property or methodused to accomplish the conversion. If the browser supports none of them, no further state- ments execute. If your script wants to check for the existence of an object s property or method, you mayalso have to check for the existence of the object beforehand if that object is not part of allbrowers object models. An attempt to reference a property of a non-existent object in a con- ditional expression generates a script error. To avoid the error, you can cascade the condi- tional tests with the help of the &&operator. The following fragment tests for the existence ofboth the document.bodyobject and the document.body.styleproperty: if (document.body && document.body.style) { // statements that work on the body s style property}

Leave a Reply