1266 Part V . Putting JavaScript to Work (Web site template)

1266 Part V . Putting JavaScript to Work Listing 47-1: Using Object Detection to Assemble an Element Object Reference function getObject(obj) { var theObj if (document.layers) { if (typeof obj == string ) { // just one layer deep return document.layers[obj] } else { // can be a nested layer return obj } } if (document.all) { if (typeof obj == string ) { return document.all(obj).style } else { return obj.style } } if (document.getElementById) { if (typeof obj == string ) { return document.getElementById(obj).style } else { return obj.style } } return null } The primary object detection for each of the three sections of this function looks for the presence of categories of objects (document.layers and document.all) or a particular method (document.getElementById()), and then this is the important part the script uses those detected objects in the statements. The script doesn t know IE4 from NN6; it does know how to derive valid references for three different object models, and employs the syntax of the first one for which the associated object property or method is supported. In practice, the order of the three sections should have no bearing on your scripts, but you should be aware of one subtlety: IE5+ can work with either of the last two sections, because those browsers detect document.alland document. getElementById as valid references. If you were to switch the position of the last two sections, then IE5+ would be using W3C DOM terminology. The results, however, are the same: A valid reference to the styleobject associated with an element. Custom APIs Notions of object detection and simplifications of your scripts come together in the final approach to building cross-browser DHTML: Writing a custom API (Application Programming Interface). A JavaScript custom API is a library of
If you are looking for affordable and reliable webhost to host and run your business application visit our ftp web hosting services.

Leave a Reply