Learning JavaScript by Shelley Powers This errata page lists errors outstanding in the most recent printing. If you have technical questions or error reports, you can send them to booktech@oreilly.com. Please specify the printing date of your copy. This page was updated May 5, 2008. Here's a key to the markup: [page-number]: serious technical mistake {page-number}: minor technical mistake : important language/formatting problem (page-number): language change or minor formatting problem ?page-number?: reader question or request for clarification Confirmed errors: {11} Insert new section, just before the section titled "JavaScript Files" header: Quirks versus Standard Mode and DOCTYPEs In the examples, the DOCTYPE used is XHTML 1.0 Transitional, even though all of the examples in the book are served as HTML, and with an .html extension. Which DOCTYPE used can influence how the page markup, CSS, and even JavaScript are interpreted. It's all based on a concept called 'quirks' mode, as compared to standards or strict mode. As browsers improve their support for markup and CSS standards, they also have to maintain backwards compatibility with pages created for older browsers. One way to do this is to render a page in such a way that it supports the browser's earlier 'quirky' behavior, in a mode called 'quirks mode'. Depending on the DOCTYPE, either a browser will render the page using an earlier interpretation of a specification, such as Internet Explorer's infamous CSS box model bug; or it will render it according to a standards-based viewpoint. The XHTML 1.0 Transitional DOCTYPE triggers standards mode for most browsers, even if the page itself isn't meant to be interpreted as an XHTML document. To actually be served up as XHTML, it needs to be passed to the browser with the XHTML MIME type, typically triggered if the extension is .xhtml. However, other modifications need to be made to the page, such as ensuring it is valid XHTML, and also modifying the opening html tag to include the tag namespace, the vocabulary where all the elements are defined: The HTML 4.01 strict DOCTYPE can also trigger standards mode for most browsers, but I don't use it because the page won't validate if proper XML markup is used. For example, the meta tag in most of the examples uses a closed tag format: This is invalid with the HTML 4.01 strict DOCTYPE. For that DOCTYPE, we need to use: Though HTML 4.01 is standard and still supported, we want to get into the habit of using proper XHTML markup as much as possible, even if the pages are still served up as HTML. This means less work when we are ready to make the step to serving pure XHTML documents, and eliminating all HTML. I mentioned earlier about how the DOCTYPE can impact on JavaScript. Well, in actuality it's how the document is served that can impact. Several of the examples use document.write or document.writeln. The reason I use these is that the example modifies the page. Later in the book, I introduce the Document Object Model (DOM) and demonstrate 'proper' methods of modifying the document. Until we reach that point, though, and in those cases where an alert box doesn't work well, the example uses document.write. Unfortunately, one of the drawbacks with using document.writeln is that it's not valid with a document that's served as XHTML. In fact, it won't work. The reason that documents rendered as XHTML don't allow document.write is that XHTML documents are considered to be valid XHTML, and won't display unless they are. If we can modify the documents with document.write, the browser has no way of knowing if the content being written to the page is valid or not. We could introduce 'bad' markup, which would render the guarantee of validity with XHTML. This same concern also exists with the use of innerHTML (which we'll look at later in the book). However, most browser do support this rather important property, and some browsers even validate the content being used to set innerHTML to ensure it's valid. For the rest of the chapter, the XHTML 1.0 Transitional DOCTYPE is used to ensure standards mode. Remember though that the namespace setting (http://www.w3.org/1999/xhtml), the extension (.xhtml), and the presence or not of document.write can impact on the document actually being served as XHTML. In the examples that can be downloaded for the book, multiple versions with different document types and extensions are provided for a couple of the examples, to demonstrate the variations discussed in this section. Each will have comments to discuss what changes were made so the document would serve correctly, as well as validate. {19} To validate, script section in Example 1-3 should be surrounded by a CDATA section, as demonstrated in Example 1-2. {31} Last two code samples; "embedded in the page" does not match code, which uses the string "embedded in page" {32} 1st paragraph; "Hi, you were here" does not match the string in the code, which is "also accessed in global2Print" (35) Example 1, in the file on O'Reilly's site (the print version is correct); burningbird,net should be burningbird.net {60} Change following sentence In particular, the equality operator is implicitly used in the switch statement, which means that both of the following cases are applicable if the switch expression evaluates to "3.0": to In particular, the switch statement does not implicitly use an equality operator, which means that an expression of 3.0 evaluates true for the first case statement, false for the second: {62} Code snippet should read: if (sValue <= 2.0) // true (63) general format of ternary operator is: condition ? value if true : value if false; [68] Example 3-8; eval ("document.writeln(document.." + docprop + ")"); should be eval ("document.writeln(document." + docprop + ")"); (71) 2nd line of sample code, halfway down; holdAnwer should be holdAnswer {75} Last Paragraph should read: "The substr method returns a substring given a starting location and length of string. (example) (following example) The substring and slice methods return a substring given a starting and ending index. (78) 2nd paragraph; The following using the explicit option: should be The following uses the explicit option: {78} All references to the regular expression '+s' and /+s/ should be replaced by 's+' and /s+/ respectively. {85} Example that reads: var newDt = new Date(1977,12,23); should be var newDt = new Date(1977,11,23); {85}, example of Date.UTC near the bottom that reads: var numMs = Date.UTC(1977,16,24,30,30,30) should read: var numMs = Date.UTC(1977,11,24,19,30,30,30) (86) Last line of example code in page should read not Also, the element directly after "" should be " (not ""). {92) The sentence before the code snippet, mid-page, that starts "In the following code snippet..." should read "In the following code snippet, a two-dimensional array is created: {93} Code snippet with var removed = fruitArray.splice(2,2,'melon,banana'); should be var removed = fruitArray.splice(2,2,'melon','banana'); (93) In the second example block; the letters in legnth are transposed, and should be "length". (94) The line: "The pop method removes the last element of the array, while the shift returns the first element." should read: "The pop method removes the last element of the array, while the shift removes the first element." {94} second and third comments in Example 4-10 should read // use shift to shift items off the array and then // now, same with unshift (95) The sentence The result of running this JavaScript is: Should be The result of running this JavaScript in our target browsers (all but IE 6.x and IE7, which don't return a length when using unshift) is: (95) Example 4-10; document.writeln("new length is " + fifoNewArray.length );i should be document.writeln("new length is " + fifoNewArray.length ); (delete the 'i' at end of line) (101) Sentence beginning with "If a condition isn't met..." Should be "If a condition is met..." (103) Code snippet that reads var func = (params) { Should read var fun = function(params) { {105} Sentence that reads Returning to the Array methods, the filter method ensures that elements are not added to any element... Should read Returning to the Array methods, the filter method ensures that elements are not added to an array... (125) The sentence that starts "When the input field is clicked..." should read "When the second input field is clicked (the first loses focus)..." (127) The sentence that starts "This makes sense, when you consider that cascade means that the lowest..." should read "This makes sense, when you consider that cascade means the highest..." {129} Example 6-7; add the following line as the first line in the function clickMe: evnt = evnt ? evnt : window.event; (135) The sentence that starts with "For the newer event systems, which use either the attachMethod or addEventListener..." Use "For newer event systems, which use either the attachEvent or addEventListener..." {135} In code snippet document.formname.addEventListener("submit", formFunction. false); The period should be a comma (138) The reference to Example 4-9 should be Example 7-1 (138) Code snippet opts[opts.length] = new Option("Option Four", "Opt 4"); Should be opts[opts.length] = new Option("Option Four", "Opt4"); (139) Example 7-2, line 11 (of the code); window.addEventListener("load",setupEvents1,false); should be window.addEventListener("load",setupEvents,false); (142) Example 7-3, line 21; } else if (document.someForm..attachEvent) { should be } else if (document.someForm.attachEvent) { (143) The sentence that starts with "Also in the example, DOM Level event handling..." Should be "Also in the example, DOM Level 2 event handling..." {144} Example 7-4; Line: var theEvent = evnt ? evnt : window.event; Should be: evnt ? evnt : window.event {175} Example 9-6 : 9th line;