Shared web hosting - 796Part IIIDocument Objects ReferenceOn the other hand, object

796Part IIIDocument Objects ReferenceOn the other hand, object models can be very literal beasts. Strictly speaking, if an elementobject presents a scriptable property that reflects an attribute for that element s tag, the firsttime a script tries to read that property, a value will be associated with that property onlyifthe attribute is explicitly assigned in the HTML code. But if you assign stylesheet settings viaremote stylesheets, the values are not explicitly set in the tag. Therefore, the stylepropertyof such an element comes up empty, even though the element is under the stylistic control ofthe remote stylesheet. If all you want to do is assign a new value to a style property, that s nota problem, because your assignment to the element object s styleproperty overrides what- ever style is assigned to that property in the remote stylesheet (and then that new value issubsequently readable from the styleproperty). But if you want to see what the current set- ting is, the initial value won t be in the element s styleobject. Microsoft (in IE5+) and the W3C DOM provide competing (and incompatible) solutions so thisproblem. IE5+ provides an extra, read-only property currentStyle that reveals the stylesheet val- ues that are currently being applied to the element, regardless of where the stylesheet defini- tions are. The currentStyleproperty returns an object that is in the same format and hasmost of the same properties as the regular styleproperty. If your audience runs browsers noearlier than IE5, you should make a habit of reading styles for an element via its currentStyleproperty. If you want a change to a styleobject s property to apply to only one element, usethe element s styleproperty to set that value; but if the change is to apply to all elementscovered by the same remote stylesheet rule, modify the styleproperty of the rule object. The W3C DOM solution is the getComputedStyle()method. Although the W3C DOM doesn t(yet) talk about a windowobject, it does describe an object (called the defaultView) whichMozilla-based browsers channel through the windowobject. To read the value of a particularstyle property being applied to an element, you first retrieve a computed stylevalue for theelement, and then read the desired CSS style property. For example, to read the font-familycurrently applied to an element whose ID is myP, use the following sequence: var elem = document.getElementById( myP ); var computedStyle = window.getComputedStyle(elem, ); var fontFam = computedStyle.getPropertyValue( font-family ); Note that you must use the CSS property name (for example, font-family) and not thescripted equivalent of that property (for example, fontFamily). Unfortunately, Safari 1.0 doesnot support the getComputedStyle()method and does not allow scripts to read stylesheetvalues set outside of a rendered element s style attribute. style Element ObjectSee Chapter 15 for items shared by all HTML elements. PropertiesMethodsEvent Handlersmediatypestyle
Looking for affordable and reliable webhost to host and run your business application? Then look no more and go to servlet web hosting services.

Leave a Reply