- variable handling given their loosely typed nature
- built-in reference types such as object and array
- object-oriented programing
- powerful aspects of function expressions
- Browser Object Model allowing interaction with the browser itself
- detecting the client and its capabilities
- Document Object Model (DOM) objects available in DOM Level 1
- how DOM Levels 2 and 3 augmented the DOM
- events, legacy support, and how the DOM redefined how events should work
- enhancing form interactions and working around browser limitations
- using the <canvas> tag to create on-the-fly graphics
- the JSON data format as an alternative to XML
- Ajax techniques including the use of XMLHttpRequest object and CORS
- complex patterns including function currying, partial function application, and dynamic functions
- offline detection and storing data on the client machine
Nicholas C. Zakas worked with the Web for over a decade. He has worked on corporate intranet applications used by some of the largest companies in the world and large-scale consumer websites such as MyYahoo! and the Yahoo! homepage. He regularly gives talks at companies and conferences regarding front-end best practices and new technology.
|Product dimensions:||7.40(w) x 9.20(h) x 1.80(d)|
About the Author
Read an Excerpt
By Nicholas C. Zakas
John Wiley & SonsCopyright © 2012 John Wiley & Sons, Ltd
All right reserved.
WHAT'S IN THIS CHAPTER?
A SHORT HISTORY
As the Web gained popularity, a gradual demand for client-side scripting languages developed. At the time, most Internet users were connecting over a 28.8 kbps modem even though web pages were growing in size and complexity. Adding to users' pain was the large number of round-trips to the server required for simple form validation. Imagine filling out a form, clicking the Submit button, waiting 30 seconds for processing, and then being met with a message indicating that you forgot to complete a required field. Netscape, at that time on the cutting edge of technological innovation, began seriously considering the development of a client-side scripting language to handle simple processing.
* The Core (ECMAScript)
* The Document Object Model (DOM)
* The Browser Object Model (BOM)
What exactly does ECMA-262 specify if it doesn't reference web browsers? On a very basic level, it describes the following parts of the language:
* Reserved words
The second edition of ECMA-262 was largely editorial. The standard was updated to get into strict agreement with ISO/IEC-16262 and didn't feature any additions, changes, or omissions. ECMAScript implementations typically don't use the second edition as a measure of conformance.
The third edition of ECMA-262 was the first real update to the standard. It provided updates to string handling, the definition of errors, and numeric outputs. It also added support for regular expressions, new control statements, try-catch exception handling, and small changes to better prepare the standard for internationalization. To many, this marked the arrival of ECMAScript as a true programming language.
ECMAScript 3.1 became ECMA-262, fifth edition, and was officially published on December 3, 2009. The fifth edition sought to clarify perceived ambiguities of the third edition and introduce additional functionality. The new functionality includes a native JSON object for parsing and serializing JSON data, methods for inheritance and advanced property definition, and the inclusion of a new strict mode that slightly augments how ECMAScript engines interpret and execute code.
What Does ECMAScript Conformance Mean?
ECMA-262 lays out the definition of ECMAScript conformance. To be considered an implementation of ECMAScript, an implementation must do the following:
* Support all "types, values, objects, properties, functions, and program syntax and semantics" (ECMA-262, p. 1) as they are described in ECMA-262.
* Support the Unicode character standard.
Additionally, a conforming implementation may do the following:
* Add "additional types, values, objects, properties, and functions" that are not specified in ECMA-262. ECMA-262 describes these additions as primarily new objects or new properties of objects not given in the specification.
* Support "program and regular expression syntax" that is not defined in ECMA-262 (meaning that the built-in regular-expression support is allowed to be altered and extended).
These criteria give implementation developers a great amount of power and flexibility for developing new languages based on ECMAScript, which partly accounts for its popularity.
ECMAScript Support in Web Browsers
By 2008, the five major web browsers (Internet Explorer, Firefox, Safari, Chrome, and Opera) all complied with the third edition of ECMA-262. Internet Explorer 8 was the first to start implementing the fifth edition of ECMA-262 specification and delivered complete support in Internet Explorer 9. Firefox 4 soon followed suit. The following table lists ECMAScript support in the most popular web browsers.
The Document Object Model (DOM)
The Document Object Model (DOM) is an application programming interface (API) for XML that was extended for use in HTML. The DOM maps out an entire page as a hierarchy of nodes. Each part of an HTML or XML page is a type of a node containing different kinds of data. Consider the following HTML page:
This code can be diagrammed into a hierarchy of nodes using the DOM (see Figure 1-2).
By creating a tree to represent a document, the DOM allows developers an unprecedented level of control over its content and structure. Nodes can be removed, added, replaced, and modified easily by using the DOM API.
Why the DOM Is Necessary
With Internet Explorer 4 and Netscape Navigator 4 each supporting different forms of Dynamic HTML (DHTML), developers for the first time could alter the appearance and content of a web page without reloading it. This represented a tremendous step forward in web technology but also a huge problem. Netscape and Microsoft went separate ways in developing DHTML, thus ending the period when developers could write a single HTML page that could be accessed by any web browser.
It was decided that something had to be done to preserve the cross-platform nature of the Web. The fear was that if someone didn't rein in Netscape and Microsoft, the Web would develop into two distinct factions that were exclusive to targeted browsers. It was then that the World Wide Web Consortium (W3C), the body charged with creating standards for web communication, began working on the DOM.
DOM Level 1 became a W3C recommendation in October 1998. It consisted of two modules: the DOM Core, which provided a way to map the structure of an XML-based document to allow for easy access to and manipulation of any part of a document, and the DOM HTML, which extended the DOM Core by adding HTML-specific objects and methods.
Whereas the goal of DOM Level 1 was to map out the structure of a document, the aims of DOM Level 2 were much broader. This extension of the original DOM added support for mouse and user-interface events (long supported by DHTML), ranges, traversals (methods to iterate over a DOM document), and support for Cascading Style Sheets (CSS) through object interfaces. The original DOM Core introduced in Level 1 was also extended to include support for XML namespaces. DOM Level 2 introduced the following new modules of the DOM to deal with new types of interfaces:
* DOM Views—Describes interfaces to keep track of the various views of a document (the document before and after CSS styling, for example)
* DOM Events—Describes interfaces for events and event handling
* DOM Style—Describes interfaces to deal with CSS-based styling of elements
* DOM Traversal and Range—Describes interfaces to traverse and manipulate a document tree
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
Table of Contents
CHAPTER 3: LANGUAGE BASICS 25
CHAPTER 4: VARIABLES, SCOPE, AND MEMORY 85
CHAPTER 5: REFERENCE TYPES 103
CHAPTER 6: OBJECT-ORIENTED PROGRAMMING 173
CHAPTER 7: FUNCTION EXPRESSIONS 217
CHAPTER 8: THE BROWSER OBJECT MODEL 239
CHAPTER 9: CLIENT DETECTION 271
CHAPTER 10: THE DOCUMENT OBJECT MODEL 309
CHAPTER 11: DOM EXTENSIONS 357
CHAPTER 12: DOM LEVELS 2 AND 3 381
CHAPTER 13: EVENTS 431
CHAPTER 14: SCRIPTING FORMS 511
CHAPTER 15: GRAPHICS WITH CANVAS 551
CHAPTER 16: HTML5 SCRIPTING 591
CHAPTER 17: ERROR HANDLING AND DEBUGGING 607
CHAPTER 19: ECMASCRIPT FOR XML 671
CHAPTER 20: JSON 691
CHAPTER 21: AJAX AND COMET 701
CHAPTER 22: ADVANCED TECHNIQUES 731
CHAPTER 23: OFFLINE APPLICATIONS AND CLIENT-SIDE STORAGE 765
CHAPTER 24: BEST PRACTICES 801
CHAPTER 25: EMERGING APIS 835
APPENDIX A: ECMASCRIPT HARMONY 857
APPENDIX B: STRICT MODE 877
Most Helpful Customer Reviews
This is an excellent book. No question 5 stars. However, it is very dense and chapters 4-8, which repeats some information in slightly different contexts, could confuse some beginners as it did me, not a beginner. But it you take your time, read slowly and thoughtfully, and re-read these chapters a of couple times, the information will start to sink in and it will start to make some sense why some information in chapters 4-8 was presented in the way it was. One thing I particularly like about the book is that the author likes to use ECMAScript as the starting reference point for discussions. By using a single starting reference point throughout the book, and by initially comparing Browser implementations against ECMAScript, instead of immediately comparing Browser implementations against one another, the topics are presented in a far more logical/organized fashion for the brain to comprehend than they would be otherwise, and the book is well positioned not only for logically comparing Browser implementations against one another, but for comparing Browser implementations against the ideal, ECMAScript, which not only gives the book an air of authority, but makes it an excellent reference. In other words, the book does not teach the language through the differences in Browser implementations. Rather, it first teaches the language through the standards, and then it teaches Browser implementations; first, by comparison to the standards, and then by comparison to other Browsers. This, I thought, was excellent idea, and it was well done. Chapters 10-21 are good at this. My only complaints about the book are that the index is weak (I started keeping one myself), and that the book's binding broke and the pages are falling out. Steve
Explains ECMAscript, DOM, & BOM in detail.
Multiple instances of 'toLocalString' instead of 'toLocaleString,' often within the same sentence, make the book appear slapdash.