Modern JavaScript
Develop and Design
Larry Ullman
Modern JavaScript: Develop and Design
Larry Ullman
Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
510/524-2221 (fax)
Find us on the Web at: www.peachpit.com
To report errors, please send a note to:
Peachpit Press is a division of Pearson Education.
Copyright 2012 by Larry Ullman
Acquisitions Editor: Rebecca Gulick
Copy Editor: Patricia Pane
Technical Reviewer: Jacob Seidelin
Compositor: Danielle Foster
Production Editor: Katerina Malone
Proofreader: Liz Welch
Indexer: Valerie Haynes-Perry
Cover Design: Peachpit Press
Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact .
Notice of Liability
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of the book, neither the author nor Peachpit Press shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it.
Trademarks
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book.
13-digit ISBN: 978-0-321-81252-0
10-digit ISBN: 0-321-81252-2
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
This book is dedicated to Doug and Christina,
and to their family and friends,
for the extraordinary, life-changing gift.
So Many, Many Thanks To...
Rebecca, Nancy, and Nancy, for working very hard to make this project happen and for their supreme flexibility. And, of course, for continuing to work with me time and again.
Patricia, for her diligent editing and attention to detail.
Jacob, for providing a top-notch technical review, and for not being afraid to say Not what I would do....
Danielle, for magically converting a handful of random materials into something that looks remarkably like an actual book.
Liz, for the sharp proofreading eye. Never too late to catch a mistake!
The indexer, Valerie, who makes it easy for readers to find what they need without wading through all of my exposition.
Mimi, for the snazzy interior and cover design work. I love the tool motif!
All the readers over the years who requested that I write this book and provided detailed thoughts as to what they would and would not want this book to be. I hope its what you were looking for!
Jonas Jacek (http://jonas.me/) for permission to use his HTML5 template.
Sara, for entertaining the kids so that I can get some work done, even if Id rather not.
Sam and Zoe, for being the kid epitome of awesomeness.
Jessica, for doing everything you do and everything you can.
Introduction
JavaScript is one of the most widely used programming languages today, found on almost every Web page (certainly all the new ones). Over the past ten years, between economic changes and expansions in how JavaScript is used, more and more Web developers and designers are expected to know this language. These facts make it all the more ironic that so few people respect JavaScript as the true programming language that it is. Furthermore, many books still present JavaScript in a legacy manner, as a technology to be used piecemeal to implement gimmicks and distractions. This book was written to address these problems, presenting JavaScript in a way that you can easily understand, actually master, and appropriately utilize as a productive asset in todays dynamic Web sites.
Who This Book Is For
This book was written primarily with two types of readers in mind:
Those who dont know JavaScript at all (and perhaps have never done any programming)
Those who may have played with JavaScript some, but dont have a solid understanding of why one does what one does in the language.
You may be a Web developer who has written code in other languages but merely dabbled with JavaScript. Or, you may be a Web designer, with a graphical focus but an increasing need to learn JavaScript. Whatever the case, if you have a sincere interest in understanding modern JavaScript and knowing how to use it, well, then this book is for you.
What You Will Learn
By reading this book, and trying the many examples, you will come to comprehend what JavaScript is and how to reliably program with it, regardless of the task. The books content is organized in three sections.
Part 1: Getting Started
The first part of the book starts with JavaScripts history and its role in todays Web. Youll also learn the fundamental terms and concepts, particularly when it comes to using JavaScript with HTML in a Web page. The last chapter in thoroughly covers the types of tools youll need to develop, design, debug, and test JavaScript code.
Part 2: JavaScript Fundamentals
The bulk of the book is in this second part, which teaches the core components of the language. These fundamentals include the kinds of data youll work with, operators and control structures, defining your own functions, handling events, and Ajax. Two chapters focus on the browser and HTML forms.
Part 3: Next Steps
All books have their limits, and this book purposefully stops short of trying to cover everything, or attempting to turn you into a true JavaScript ninja. But in the third part of the book, you will be introduced to what your next logical steps should be in your development as a JavaScript programmer. One chapter is on frameworks, another is on advanced JavaScript concepts, and a third walks through a real-world integration of JavaScript and PHP for a practical Web application.
The Corresponding Web Site
My Web site can be found at www.LarryUllman.com. To find the materials specific to this book, click on Books By Topic at the top of the page, and then select JavaScript > Modern JavaScript: Develop and Design. On the first page that comes up you will find all of the code used in the book. There are also links to errata (errors found) and more information that pertains directly to this book.
The whole site is actually a WordPress blog and youll find lots of other useful information there, in various categories. The unique tag for this book is jsdd, meaning that www.larryullman.com/tag/jsdd/ will list everything on the site that might be useful and significant to you. While youre at the site, I recommend that you also sign up for my free newsletter, through which I share useful resources, answer questions, and occasionally give away free books.
The book has a corresponding support forum at www.LarryUllman.com/forums/. You are encouraged to ask questions there when you need help. You can also follow up on the Review and Pursue sections through the forums.