Advanced DOM Scripting
by Jeffrey Sambells and Aaron Gustafson
I, Michael Parker, own this book and took these notes to further my own learning. If you enjoy these notes, please purchase the book!
Chapter 1: Do It Right with Best Practices
- pg 11: Instead of using inline event attributes, use unobtrusive techniques to add the event handlers when the window loads.
- pg 14: Instead of browser sniffing to find what features you can use, use capability detection and check for the existence of methods directly.
- pg 16: Don’t require Javascript for content, as search engine crawlers will miss it.
- pg 21: When creating your namespace with its methods, check that it doesn’t already exist if your library is already included from another file.
- pg 28: The display property can be toggled between
noneand empty (the string''), which is the default. - pg 31: Prefer single quotes in Javascript, and leave the double quotes for XHTML attributes as its specification requires.
- pg 39: The
hasOwnPropertymethod returnstrueif the object has the property or method specified, ignoring any inherited properties or methods.
Chapter 2: Creating Your Own Reusable Objects
- pg 63: Static members exist only on a specific instance of an object, by manual assignment and not through any prototype property.
- pg 67: Private methods are defined within a constructor or another method, but not available externally through any instance.
- pg 68: Privileged methods are defined within the constructor using the
thiskeyword, and can access private methods while being publicly accessible. - pg 70: When you declare function
foo() { ... }, you can call the function before its definition. If you use varfoo = function() { ... }, you can’t. - pg 74: When a method of an object is used as an event handler, you can bind
thisto the object again usingcallorapply.
Chapter 3: Understanding the DOM2 Core and DOM2 HTML
- pg 91: DOM Level 2 builds on Level 1 by specifying mouse-related events and the ability to access and manipulate CSS styles.
- pg 94: Internet Explorer 7 only supports some of DOM Level 1, but supports some features of Level 2 (like events) in its own way.
- pg 101: IE will only create a text node in the DOM if it contains something other than whitespace.
- pg 105: The
nodeValueproperty of aNodereturns the value of an attribute node or the content of a text node, andnullin most other cases. - pg 111: Attributes are implementations of the Core
Attrinterface and are contained in aNamedNodeMapin a node’sattributeproperty. - pg 113: The
attributes.getNamedItemmethod will work on any node, while thegetAttributemethod only works on instances ofElement. - pg 117: Don’t modify the prototype of any DOM classes like
Node; it’s flaky across browsers, and may override any future additions to the DOM specification. - pg 122: The
Documentclass has agetElementsByTagNamelikeElementdoes, but technically it’s different; it also addsgetElementById. - pg 126: The
HTMLDocumentclass inherits fromDocument, and adds properties liketitle,referrer,body,images,forms, andanchors. - pg 127: The
HTMLElementclass inherits fromElement, and adds properties likeid,title(for tooltip rollovers), andclassName. - pg 133: The DOM names CSS properties like
font-sizeasfontSize, andclassasclassName.
Chapter 4: Responding to User Actions and Events
- pg 154: The
mousemoveevent has no specified period or distance in which it fires. - pg 156: A
clickanddblclickevent will only happen if the mouse remains stationary; regardless of the mouse motion, amousedownandmouseupevent occurs. - pg 159: Key-related events apply only to the
documentobject. - pg 161: The
focusandblurevens only apply tolabel,input,select,textarea, andbuttonform elements. - pg 182: Event listeners registered using the W3C event model do not necessarily execute in the order in which they’re added.
- pg 183: Beware that the
loadevent is only executed after all the images have been loaded as well. - pg 196: A simple drag-and-drop effect can be created with a
mousemoveevent listener that moves an element to the cursor’s location. - pg 197: Keyboard commands are not in the DOM2 Events specification, but all browsers supply the Unicode value of a key press in the
keyCodeproperty.
Chapter 5: Dynamically Modifying Style and Cascading Style Sheets
- pg 205:
CSSStyleSheetrepresents a stylesheet withCSSStyleRules, while theCSSStyleDeclarationclass represents an object’s style property. - pg 214: A
CSSStyleDeclarationinstance of an element only contains the inline style properties, and hence does not contain the overall computed style. - pg 216: The only time mixing style and scripting is acceptable practice is in the case of positioning, e.g. drag-and-drop.
- pg 220: Use the
classNameproperty to assign classes, assetAttributerequires an attribute name of'class'in W3C and'className'in IE. - pg 222: Setting the
relattribute of a stylesheet link to"alternate stylesheet"will load the stylesheet but disable it; Javascript can enable it later. - pg 228: A new stylesheet can be used by dynamically creating a new link element referencing the stylesheet and adding it to the
headtag. - pg 232: The W3C browsers contain the rules in a stylesheet in the variable
cssRules, while IE contains them inrules. - pg 233: The W3C
insertRulemethod combines the selector name and its definition in a single argument, while theaddRulemethod of IE combines them. - pg 238: The computed style of an element is accessible through
document.defaultView.getComputedStylein W3C, andelement.currentStylein IE.
Chapter 7: Adding Ajax to the Mix
- pg 286: IE 7 and later support the
XMLHttpRequestobject, while earlier versions must use theMicrosoft.XMLHTTPActiveX component. - pg 289: The
onreadystatechangelistener doesn’t receive any arguments, so theXMLHttpRequestobject must be defined in the same scope for it to be available. - pg 289: If the response has MIME type
application/xml, the response will be interpreted as XML and not HTML, so the DOM2 HTML methods won’t work. - pg 291: Within the
onreadystatechangeproperty, thethiskeyword refers to the method itself and not theXMLHttpRequestobject. - pg 298: To parse JSON from an Ajax response, prefer a JSON parser over
evalunless the information in the response is entirely controlled by you. - pg 307: To bypass cross-site restrictions, dynamically generated
scriptelements can run with a source from any domain and their Javascript will run normally. - pg 328: Replies to concurrent requests may come back out of order; guard against this causing inconsistent application behavior.