dom-scripting.markdown
January 15, 2014 ยท View on GitHub
DOM Scripting
by Jeremy Keith
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
- ECMAScript is the proper name of Javascript, which is not related to Java.
- DHTML was a shorthand term for describing the combination of HTML, CSS, and Javascript.
- DOM is generic: a model that can be used by any programming language to manipulate any document in any markup language.
Chapter 2
- Semicolons aren't required in Javascript unless multiple statements appear on one line, but are good practice.
- Use C or C++ commenting styles, which are
//commentand/*comment*/, respectively. - Declare numeric or associative arrays by using the same
Arrayclass. - Prepending a variable declaration with
varwill treat the variable as a local variable. - Objects have properties (variables) and methods (actions as functions).
- Native objects are those part of the Javascript library, such as
Array,Date, andMath. - Host objects are those supplied by the web browser, such as
Form,Image, andElement.
Chapter 3
- The properties and methods of the window object are often referred to as the Browser Object Model.
- Element attributes are represented by attribute nodes, or children of the element, in the DOM.
- Method
getElementsByTagNameaccepts the wildcard value*. - Method
getAttributewill returnnullif an attribute does not exist.
Chapter 4
- In an
onclickhandler, you can use thethiskeyword to refer to the element node. - Returning
falsefrom theonclickhandler of animgelement will disable the default behavior of following thesrcURL. - The
bodyelement of the document can be retrieved usingdocument.body. - Whitespace and line breaks between elements in the HTML source are interpreted in nodes in the
childNodesarray. - The
nodeTypeattribute has 12 values, where1is an element node,2is an attribute node, and3is a text node. - The
nodeValueattribute contains the text value of a text node.
Chapter 5
- Making your web site navigable by users without Javascript is called graceful degradation.
- The JavaScript pseudo-protocol allows invoking JavaScript from within a link, but does not allow graceful degradation.
- Likewise, defining a link as
#and using an inline event handler does not allow graceful degradation. - Progressive enhancement is using JavaScript and the DOM to enhance sites that are already functional without them.
- When
window.onloadis invoked, the document (and hence DOM) within it is guaranteed to exist. - Testing whether the browser supports some DOM property or method in a conditional is called object detection.
Chapter 6
- When applying an anonymous function, the
thiskeyword will refer to the element it is bound to. - To invoke multiple functions on loading, you must bind a single function to
window.onloadthat invokes each. - To be cautious, test not only whether DOM methods exist, but expected DOM nodes and their attributes exist.
- Don't use the
onkeypressevent handler -- it can be invoked even if the user presses the Tab key. - The
onclickevent handler is also invoked if you press Return while tabbing from link to link. - HTML-DOM extends the DOM Core with properties like
document.formsanddocument.images.
Chapter 7
- You must invoke the
document.writemethod from ascriptelement in the body, which is obtrusive, so avoid it. - The
innerHTMLelement isn't a web standard, and referencing the inserted elements still requires the DOM, so avoid it. - Both
document.writeandinnerHTMLwon't work with XHTML served with MIME typeapplication/xhtml+xml. - The
createElementmethod creates an element not attached to the DOM tree, called aDocumentFragment. - Text elements are created with method
createTextMode, notcreateElement. - There is an
insertBeforemethod for inserting elements, but you have write your owninsertAftermethod.
Chapter 8
- An abbreviation is any shortened version of a word, while an acronym is an abbreviation pronounced as a word.
- You can iterate through the keys of an
Arrayobject using the constructfor (variable in array). - Sniffing for a specific browser name and version number is bound to cause problems and convoluted code.
- Using DOM you can add meaningful content from attributes that browsers typically ignore, like
citeforblockquote. - Convention for the
accesskeyattribute is1for home,4for search, and9for contact. - The accessibility statement lists which access keys have been assigned on a page.
Chapter 9
- Every element has a
styleproperty -- it is an object, and not a simple string. - CSS properties are converted to camel case, so the
font-familyproperty becomeselement.style.fontFamily. - Usually style properties are returned in the same units in which they are set, for example
emvspx. - The DOM can only return inline style information, not style applied through external CSS files or
styletags. - But the DOM can retrieve styles that the DOM itself assigns through the
styleproperty. - CSS pseudo-classes and DOM scripting can overlap when styling elements based on state, like
:hoverversus:onmouseover. - The DOM can apply styles defined in CSS files by setting the element's
classattribute, or itsclassNamestyle property.
Chapter 10
- Elements have a position of
staticby default, but by usingrelativewe can use thefloatproperty on them. - The
setTimeoutmethod returns a handle to the function that can be cancelled usingclearTimeout. - Methods
parseIntandparseFloatextract the number starting any string, e.g.39 stepsconverts to39. - The
overflowproperty specifies how to handle content that is larger than the containing element. - To persist variables between method calls, set them as element properties instead of making them global.
Chapter 11
- You can split your CSS into multiple files: one for layout, one for color, and one for typography.
- The address of the current window can be retrieved through property
window.location.href. - To frame a slideshow element, make the frame image an absolutely positioned child with a high z-index.
- Invoking the
focusmethod on a form text element will place the cursor inside it. - The
form.elementsarray contains only descendant form elements, which differs fromform.childNodes.
Reference
- If the boolean argument to
cloneNodeistrue, all children nodes are copied; iffalse, only its attributes are. - The newly created node returned by
cloneNodeis not automatically added to the document. - Methods
appendChild,insertBefore, andreplaceChildwill move elements that already exist in the DOM. - If the
hasChildNodesproperty isfalse,childNodesis an empty array, andfirstChildandlastChildarenull. - The
nodeNameproperty for a element node is its tag type, and for an attribute node is the attribute name. - The
nodeValueproperty for an attribute node is its value, and for a text node is its text, but isnullfor element nodes. - You can't set
nodeValueif it is alreadynull, i.e. you can't assign a value to element nodes. - The nodes adjacent to an element node can be retrieved with
nextSiblingandpreviousSibling.