JavaScript Step by Step
Steve Suehring
Published by Microsoft Press
I would like to dedicate this book to Mom and Dad.
S TEVE S UEHRING
Special Upgrade Offer
If you purchased this ebook directly from oreilly.com, you have the following benefits:
DRM-free ebooksuse your ebooks across devices without restrictions or limitations
Multiple formatsuse on your laptop, tablet, or phone
Lifetime access, with free updates
Dropbox syncingyour files, anywhere
If you purchased this ebook from another retailer, you can upgrade your ebook to take advantage of all these benefits for just $4.99. to access your ebook upgrade.
Please note that upgrade offers are not available from sample content.
A Note Regarding Supplemental Files
Supplemental files and examples for this book can be found at http://examples.oreilly.com/9780735665934-files/. Please use a standard desktop web browser to access these files, as they may not be accessible from all ereader devices.
All code files or examples referenced in the book will be available online. For physical books that ship with an accompanying disc, whenever possible, weve posted all CD/DVD content. Note that while we provide as much of the media content as we are able via free download, we are sometimes limited by licensing restrictions. Please direct any questions or concerns to .
Introduction
Much has changed since the first edition of JavaScript Step by Step was written in 2007: the underlying JavaScript specification received a major update; Microsoft released new versions of Internet Explorer and Windows; the Chrome browser came of age, as did mobile web usage; and JavaScript development frameworks have matured and are now ubiquitous.
This third edition of JavaScript Step by Step builds on the foundation laid down by the first two editions. While the underlying architecture of the JavaScript language has remained largely the same, use of JavaScript has become pervasive, with huge year-over-year increases and an expanded importance to developers. With that in mind, the layout and coverage of the book have also remained largely the same, with some notable exceptions. The book now places extra emphasis on JavaScript event handling and the use of jQuery to speed development. The book also includes a final section on Windows 8 development using JavaScript. However, this book is most definitely not Microsoft-centric.
One of the first things I asked prior to accepting the offer to write JavaScript Step by Step was whether it had to focus on Microsoft products. The answer was a firm no. The book was and is intended to be a general tutorial on using JavaScript, including best practices for using JavaScript on the web.
The biggest influence Microsoft has had on this book was to make sure that I used the term Internet Explorer when referring to IE. Youll see this absence of bias reflected throughout the book, which includes exercises built using plain text editors as well as full-featured development tools. While its true that most of the screen shots show Internet Explorer (I almost said IE), the code was also tested across several other browsers, including Chrome and Firefox. In fact, much of the books code was written in Vim, and tested in a cross-browser fashion.
Throughout the book, youll find highlights and additions for the new features in the latest version of JavaScript. Also, the examples used in the book received greater scrutiny in multiple browsers to reflect the reality of todays web landscape. Reader feedback from the earlier editions is reflected in the content of this edition and was the impetus for adding jQuery and emphasizing event handling.
Housekeeping aside, this book provides an introductory look at JavaScript, including some of its core functions as well as features and paradigms such as Asynchronous JavaScript and XML (AJAX).
The first part of the book examines JavaScript and helps you get started developing JavaScript applications. You dont need any specific tools for JavaScript development, so youll see how to create JavaScript files in Microsoft Visual Studio, and in , in Eclipse and in Notepad (or any other text editor). The book examines JavaScript functions and the use of JavaScript in the browser, along with the aforementioned jQuery. Finally, the book provides coverage of Windows 8 app development using HTML, CSS, and JavaScript.
Who should read this book
This book is for beginning JavaScript programmers or people who are interested in learning the basics of modern JavaScript programming: the language syntax, how it works in browsers, what the common cross-browser problems are, and how to take advantage of AJAX and third-party libraries such as jQuery to add interactivity to your webpages.
Assumptions
This book expects that you have at least a minimal understanding of concepts surrounding web development. You should be at least somewhat familiar with HTML. CSS is also helpful to know, but neither HTML nor CSS are required prerequisite knowledge for completing this book. The examples shown provide all the HTML and CSS whenever necessary.
Who should not read this book
This book isnt meant for experienced JavaScript programmers.
Organization of this book
This book is divided into four sections, each of which focuses on a different aspect of JavaScript programming. , shows how to create a Windows 8 app with HTML, CSS, and JavaScript.
Conventions and features in this book
This book takes you step by step through the process of learning the JavaScript programming language. Starting at the beginning of the book and following each of the examples and exercises will provide the maximum benefit to help you gain knowledge about the JavaScript programming language.
If you already have some familiarity with JavaScript, you might be tempted to skip the first chapter of this book. However, to create JavaScript programs.
The book contains a Table of Contents that will help you to locate a specific section quickly. Each chapter contains a detailed list of the material that it covers.
The coverage of Windows 8 app development is limited to the final section of the book, so if youre not interested in making a Windows 8 app (its really easy) then you can safely skip that last section without missing any of the valuable information necessary to program in JavaScript for the web. If youre looking for a more comprehensive book on Windows 8 development with HTML5 and JavaScript, a beginners book, Start Here! Build Windows 8 Apps with HTML5 and JavaScript will be available from Microsoft Press in May (pre-order here: http://oreil.ly/build-w8-apps-HTML5-JS ).
In addition, you can download the source code for many of the examples shown throughout the book.
System requirements
You will need the following hardware and software to complete the practice exercises in this book:
An operating system capable of running a web server. For the section on Windows 8 development, youll need Windows 8, but none of the other examples require Windows.
A text editor such as Notepad, Vim, or an Integrated Development Environment (IDE) such as Visual Studio or Eclipse. For Windows 8 development, youll specifically need Visual Studio 2012 for Windows.
An Internet connection so you can download software and chapter examples.
Code Samples
Most of the chapters in this book include exercises that let you interactively try out new material learned in the main text. The code for those exercises and many other examples can be downloaded from: