Archive for September, 2007

179Chapter 14Document Object Model EssentialsMore importantly, when you (Web file server)

Monday, September 17th, 2007

179Chapter 14Document Object Model EssentialsMore importantly, when you consider the changes to referencing other elements in the W3CDOM (discussed in the next section), we re lucky that the old ways of referencing objectssuch as forms, form control elements, and images still work. Had the working group beenplanning from a clean slate, it is unlikely that the documentobject would have been givenproperties consisting of arrays of forms, links, and images. The only potential problems you could encounter with your existing code have to do with ahandful of properties that used to belong to the documentobject. In the new DOM, fourstyle-related properties of the documentobject (alinkColor, bgColor, linkColor, andvlinkColor) become properties of the bodyobject (referenced as document.body). In addi- tion, the three link color properties pick up new names in the process (aLink, link, vLink). It appears, however, that for now, IE6 and Moz1 maintain backward compatibility with theolder document object color properties. Also, note that the DOM specification concerns itself only with the document and its content. Objects such as window, navigator, and screenare not part of the DOM specification throughLevel 2. Scripters are still at the mercy of browser makers for compatibility in these areas, butthe windowobject (or its equivalent) likely will be added to the W3C DOM Level 3. What isn t availableAs mentioned earlier, the W3C DOM is not simply a restatement of existing browser specifica- tions. Many convenience features of the IE and NN object models do not appear in the W3CDOM. If you develop Dynamic HTML content in IE4+ or NN4, you have to learn how to getalong without some of these conveniences. Navigator 4 s experiment with the tag was not successful in the W3C process. As aresult, both the tag and the scripting conventions surrounding it do not exist in the W3C DOM. To some scripters relief, the document.layerNamereferencing scenario (even more complexwith nested layers) disappears from the object model entirely. A positioned element is treatedas just another element that has some special stylesheet attributes that enable you to move itanywhere on the page, stack it amid other positioned elements, and hide it from view. Among popular IE4+ features missing from the W3C DOM are the document.allcollection ofHTML elements and four element properties that facilitate dynamic content: innerHTML, innerText, outerHTML, and outerText. A new W3C way provides for acquiring an array ofall elements in a document, but generating HTML content to replace existing content or beinserted in a document requires a tedious sequence of statements (see the section NewDOM concepts later in this chapter). Mozilla, however, has implemented the innerHTMLproperty for HTML element objects in Mozilla-based browsers (Moz1+). New HTML practicesExploitation of Dynamic HTML possibilities in both IE4+ and the W3C DOM relies on someHTML practices that may be new to long-time HTML authors. At the core of these practices(espoused by the HTML 4.0 specification) is making sure that all content is within an HTMLcontainer of some kind. Therefore, instead of using the

tag as a separator between blocksof running text, surround each paragraph of the running text with a

tag set. Ifyou don t do it, the browser treats each

tag as the beginning of a paragraph and ends theparagraph element just before the next

tag or other block-level element. While recent browsers continue to accept the omission of certain end tags (for td, tr, and lielements, for instance), it is best to get in the habit of supplying these end tags. If for no otherreason, they help you visualize where an element s sphere of influence truly begins and ends.

178Part IIIDocument Objects ReferenceTo make (Java web server) this work effectively,

Sunday, September 16th, 2007

178Part IIIDocument Objects ReferenceTo make this work effectively, the working group divided the DOM specification into two sec- tions. The first, called the Core DOM, defines specifications for the basic document structurethat both HTML and XML documents share. This includes notions of a document containingelements that have tag names and attributes; an element is capable of containing zero ormore other elements. The second part of the DOM specification addresses the elements andother characteristics that apply only to HTML. The HTML portion inherits all the features ofthe Core DOM, while providing a measure of backward compatibility to object models alreadyimplemented in legacy browsers and providing a framework for new features. It is important for veteran scripters to recognize that the W3C DOM does not specify all fea- tures from existing browser object models. Many features of the IE4 (and later) object modelare not part of the W3C DOM specification. This means that if you are comfortable in the IEenvironment and wish to shift your focus to writing for the W3C DOM spec, you have to changesome practices as highlighted in this chapter. Navigator 4 page authors lose the tag(which is not part of HTML 4.0 and likely will never see the light of day in a standard) as well asthe layer object. In many respects, especially with regard to Dynamic HTML applications, theW3C DOM is an entirely new DOM with new concepts that you must grasp before you can suc- cessfully script in the environment. By the same token, you should be aware that whereas Mozilla-based browsers go to greatlengths to implement all of DOM Level 1 and most of Level 2, Microsoft (for whatever reason) features only a partial implementation of the W3C DOM through IE5.5. Although IE6 imple- ments more W3C DOM features, some important parts, notably W3C DOM events, are miss- ing. Other modern browsers, such as Safari, provide basic W3C DOM support, but have notyet caught up with Mozilla levels of DOM support. DOM levelsLike most W3C specifications, one version is rarely enough. The job of the DOM workinggroup was too large to be swallowed whole in one sitting. Therefore, the DOM is a continuallyevolving specification. The timeline of specification releases rarely coincides with browserreleases. Therefore, it is very common for any given browser release to include only some ofthe most recent W3C version. The first formal specification, DOM Level 1, was released well after NN4 and IE4 shipped. The HTML portion of Level 1 includes DOM Level 0. This is essentially the object model asimplemented in Navigator 3 (and for the most part in Internet Explorer 3 plus image objects). Perhaps the most significant omission from Level 1 is an event model (it ignores even thesimple event model implemented in NN2 and IE3). DOM Level 2 builds on the work of Level 1. In addition to several enhancements of both theCore and HTML portions of Level 1, Level 2 adds significant new sections (published as sepa- rate modules) on the event model, ways of inspecting a document s hierarchy, XML names- paces, text ranges, stylesheets, and style properties. Work on Level 3 is under way, but verylittle of it has yet reached browsers. What stays the sameBy adopting DOM Level 0 as the starting point of the HTML portion of the DOM, the W3C pro- vided a way for a lot of existing script code to work even in a W3C DOM-compatible browser. Every object you see in the original object model starting with the documentobject (seeFigure 14-1) plus the image object are in DOM Level 0. Almost all of the same object proper- ties and methods are also available.

177Chapter 14Document Object Model EssentialsInternet (Web site template) Explorer 5+ ExtensionsWith

Saturday, September 15th, 2007

177Chapter 14Document Object Model EssentialsInternet Explorer 5+ ExtensionsWith the release of IE5, Microsoft built more onto the proprietary object model it launched inIE4. Although the range of objects remained pretty much the same, the number of properties, methods, and event handlers for the objects increased dramatically. Some of those additionswere added to meet some of the specifications of the W3C DOM (discussed in the next sec- tion), occasionally causing a bit of incompatibility with IE4. But Microsoft also pushed aheadwith efforts for Windows users only that may not necessarily become industry standards: DHTML behaviors and HTML applications. A DHTML behavioris a chunk of script saved as an external file that defines some action(usually a change of one or more style properties) that you can apply to any kind of element. The goal is to create a reusable component that you can load into any document whose ele- ments require that behavior. The behavior file is known as an HTML component, and the filehas an .htcextension. Components are XML documents whose XML tags specify events andevent-handling routines for whatever element is assigned that behavior. Script statements in.htcdocuments are written inside