by Peter-Paul Koch
I, Michael Parker, own this book and took these notes to further my own learning. If you enjoy these notes, please purchase the book!
- pg 6: A fat client emphasizes JS and reduces load times, thereby increasing usability; a thin client uses little JS and thereby increases accessibility.
- pg 10: ECMA standardized the JS core language, but the W3C created the DOM specification which includes event handling and CSS modification.
- pg 15: JS cross-window communication is allowed only when the pages in both windows come from the same domain (not even related subdomains).
- pg 15: Documents in different subdomains can communicate with each other by setting document.domain to some value part of their real domains.
- pg 29: In 2002 Stuart Langridge coined the term "unobtrusive scripting" which is where the JS will hook itself into a page for enhanced usability.
- pg 35: Add and remove DOM elements in a form with JS instead of simply hiding them because hidden fields are submitted too.
- pg 38: Don't use the
javascriptpseudo-protocol link which is a no-op for noscript users; instead, add anonclickhandler with a default value. - pg 41: The JS event equivalents of CSS
:hoverare notmouseoverandmouseout, butmouseenterandmouseleavewhich are only supported by IE. - pg 41: A CSS dropdown menu triggered by a
li:hoverstyle cannot accommodate keyboard users because they can't focus onli, whereas a JS one could. - pg 46: No
mouseoverevent will ever trigger if the user doesn't use a mouse -- to assist keyboard users, use afocusevent. - pg 47: Screen readers read a page from top to bottom, so modifying a part of the page that is already read will be missed by the reader.
- pg 53: If you have a link with an
onclickhandler but no sensiblehrefvalue, just generate the link with JS so noscript users never see it. - pg 55: If redirecting a user, use
location.replace()instead of assigninglocation.hrefto gracefully handle the use clicking the Back button. - pg 57: Don't use the
noscripttag -- the user agent may know enough JS to ignore it, but not all the functionality you actually need.
- pg 69: The rendering engine for Internet Explorer is called Trident.
- pg 77: Don't write a script for one browser first and add support for other browsers later on; solve nasty incompatibilities at the start, not the end.
- pg 81: When doing object detection, always try and use the standards-compliant method first, and fall through to any proprietary ones.
- pg 88: Don't do browser detection; but if you need some sort of introspection, always use
navigator.userAgent. - pg 97: The
alertmethod is useful for debugging, but tiresome if in a loop and you've found the problem; in this case, useconfirmto break out of it.
- pg 112: Custom attributes fail (X)HTML validation, can can encode name-value pairs for hooks not easily expressed in a
classattribute. - pg 121: The
scripttag inheadcan include scripts from other domains, since this is decided by the Webmaster and implicitly judged safe. - pg 122: If using XHTML, the content of the
scripttag must be defined asCDATA, which tells the parser not to parse the contents. - pg 128: Running JS initialization code using
window.onloadwaits for all images to load; to avoid this, put thescripttag before the closingbodytag.
- pg 155: The value
undefinedis returned when a) a variable is declared but unassigned; b) you access anundefinedproperty of an object; c) a function argument is defined, but no value is passed it to it. - pg 155: The
typeofoperator yields"function"on functions when they're really objects, and yields"object"when applied tonull. - pg 160: When converting to
boolean,null,undefined,0,NaN, and the empty string arefalse; everything else istrue. - pg 160: To convert a string to a number, multiply by
1or subtract0; to convert a variable to a boolean, apply the!operator twice. - pg 166: Comparison operators will interpret (i.e. convert) their operands to numbers if they can, otherwise they perform lexicographic comparison.
- pg 172: JS makes no distinction between integers and floating point values; they're both numbers.
- pg 176: The
toFixedmethod of a number converts it to a string with the specified number of decimals. - pg 177: The
parseIntandparseFloatwill parse the leading number of a string that can contain non-numeric characters. - pg 189: The operators
&&and||don't returntrueorfalse, but the value of the last expression they evaluated. - pg 198: The value after a
casestatement must have the same data type as the variable in theswitch; no data-type conversion happens. - pg 208: You can
continueorbreakfrom an outer loop by using a label. - pg 209: Unlike Java, you can not catch particular exception types -- you can only do
catch (e), which is the exception object. - pg 217: Javascript functions run in the scope in which they are defined, not in the scope from which they're executed.
- pg 218: A nested function has access only to the final value of a local variable; beware of them accessing loop counters, etc.
- pg 221: Predefined JS objects are either internal (e.g. the
Stringobjects) or host objects (created by Javascript's host, the browser). - pg 225: The
thiskeyword always refers to the object the method is defined on. - pg 226: Client-side Javascript binds treats global variables as properties of the
windowobject. - pg 235: The W3C DOM
nodeListdata type resembles an array, but is read-only and lacks all the methods defined byArray.
- pg 242: When the user loads a new page, the
windowobject is wiped clean but not destroyed, so references remain intact. - pg 245: After one window opens another, if the browser hasn't had time to download the page yet, its properties may be unavailable.
- pg 251: The
window.openmethod returns a reference to the new window, which has a propertyopenerreferring to the caller window. - pg 255: When the user closes a window, its
windowobject remains available, but itsclosedproperty is set totrue. - pg 257: Every
windowobject has aclosemethod; some browsers won't execute it, others open a dialog box for permission. - pg 261: You can only read the length of
window.history, not its URLs; to navigate, use itsgo,back, andforwardmethods. - pg 265: The
focusmethod brings a window to the front of the stacking order, whileblurhides the window. - pg 268: The first argument of
setTimeoutcan be a string that is interpreted as a JS command, or a plain function reference. - pg 270: The browser may set
lastModifiedas either January 1 1970 or the current date if the server does not send the header. - pg 275: A cookie is stored in a name/value pair, where the name is what makes a cookie unique for a given path.
- pg 276: If a page sets a cookie, only pages in that directory and its subdirectories can access them unless its path is changed.
- pg 277: If a cookie is created in a subdomain, but its domain is changed to the top-level domain, it is now associated with all subdomains.
- pg 278: When
document.cookieis assigned to, a new cookie is created; when read, it contains all cookies separated by semicolons. - pg 281: A resource loaded from a third party domain allows that domain to set cookies, but modern browsers refuse them by default.
- pg 288: The
clickevent is the only accessible mouse event, firing when the user has focused on an element and presses Enter.
- pg 356: In XML,
getElementByIdsearches through attributes with the type (not the name) id, as defined in the DTD. - pg 359: The
childrennode list contains only the element nodes (not text nodes) inchildNodes, but it is not part of the DOM or fully supported. - pg 362: The
document.documentElementand thedocument.bodyallow access to thehtmlandbodytags, respectively. - pg 375: The
document.createTextNodeargument cannot contain HTML entities; instead, use theinnerHTMLproperty. - pg 376: The
cloneNodemethod will not clone event handlers, so you must redefine them on the clone. - pg 378: To work with IE, add generated table elements to a
tbody, and create radio buttons withinnerHTMLinstead of the DOM. - pg 382: Assigning
innerHTMLis 3 to 30 times faster than using pure DOM methods. - pg 392: HTML attributes are nodes in the DOM, while JS properties belong to the object representing the tag.
- pg 393: Some JS properties are mapped to HTML attributes, e.g. the inline style, event handlers, and form fields, links, and images.
- pg 395: IE does not support empty text nodes, while all other browsers do.
- pg 398: The best defense against empty text nodes is to not use
previousSibling,nextSibling, orchildNodesat all. - pg 401: A
nodeListis dynamic, so beware of introducing errors by adding or removing elements as you iterate over it. - pg 408: In the level 0 DOM, the
elementsassociative array of a form maps a value to all radio buttons with that value as its name. - pg 410: In the level 0 DOM, for a
selectbox, assigningnullto an element deletes it, while assigning an element inserts it. - pg 416: You can check whether an element is in DOM hyperspace by checking if it has a parent node.