Javascript for Absolute Beginners Companion Companion - - - -
Total Page:16
File Type:pdf, Size:1020Kb
CYAN YELLOW MAGENTA BLACK PANTONE 123 C BOOKS FOR PROFESSIONALS BY PROFESSIONALS® THE EXPERT’S VOICE® IN WEB DEVELOPMENT Companion eBook Available JavaScript for Absolute Beginners JavaScript Dear Reader, Even though I’ve been hand-coding JavaScript for twelve years, I haven’t forgot- ten what it’s like to be just starting out. With this in mind, I wrote this book in a friendly, conversational style for web designers new to both JavaScript and pro- gramming. I assume a familiarity with HTML and CSS, but nothing more. I’m also aware that just passively staring at code samples in a book is no way to learn how to program. So, as we explore ECMAScript, a standard defining JavaScript’s core syn- tax, and DOM, a standard providing features for working with HTML, CSS, and events, you will enter and run hundreds of code snippets to see exactly how the techniques you’re learning work in the real world. All this will be done in the safe- for ty of the JavaScript console of Firebug, a free add-on to Firefox for PC, Mac, or Linux. Then in the last two chapters of the book, you’ll leave the nest and hand- Absolute Beginners code a real-world application in your preferred text editor. That application will contain features like drag-and-drop, animated scrolling, sprites, and skin swap- JavaScript ping. Moreover, it will dynamically add five galleries either by way of Ajax and data encoded JSON, XML, and HTML, or by dynamic script insertion and JSON with Padding (JSON-P). Don’t worry if that sounds a bit bewildering now, it’ll all make sense soon enough! Finally, you’ll make your script snappier, by incorporating leading-edge optimizations, such as advance conditional definition, lazy loaders, reverse for Absolute Beginners loops, closure, minimizing reflows, and thread yielding. And even some new features from DOM3 and HTML5 that Explorer, Firefox, Safari, and Opera now implement. So, by the end of the book, you will know how to hand-code ultra- responsive interfaces. And you’ll have the kinds of JavaScript tools in your pocket that employers crave. Terry McNavage Companion eBook Learn to write effective JavaScript code from scratch THE APRESS ROADMAP Pro Pro JavaScript Beginning JavaScript RIA JavaScript for Absolute Beginners HTML5 and CSS3 See last page for details Techniques with Mootools on $10 eBook version McNavage SOURCE CODE ONLINE www.apress.com ISBN 978-1-4302-7219-9 Terry McNavage 52999 US $29.99 Shelve in Web Development\JavaScript User level: 9 781430 272199 Beginner www.allitebooks.com this print for content only—size & color not accurate 7.5 x 9.25 spine = 0.9375" 504 page count www.allitebooks.com JavaScript for Absolute Beginners ■ ■ ■ Terry McNavage i www.allitebooks.com JavaScript for Absolute Beginners Copyright © 2010 by Terry McNavage All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302-7219-9 ISBN-13 (electronic): 978-1-4302-7218-2 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. President and Publisher: Paul Manning Lead Editors: Ben Renow-Clarke, Matthew Moodie Technical Reviewers: Kristian Besley, Rob Drimmie, Tom Barker Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Mary Tobin Copy Editor: Kim Wimpsett Compositor: MacPS, LLC Indexer: Toma Mulligan Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media, LLC., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer- sbm.com, or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress 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 information contained in this work. ii www.allitebooks.com To the Little Flower, St. Thérèse de Lisieux, for sending me this rose. iii www.allitebooks.com Contents at a Glance ■Contents ................................................................................................................ v ■About the Author ................................................................................................ xiii ■About the Technical Reviewers .......................................................................... xiv ■Acknowledgments ............................................................................................... xv ■Preface ............................................................................................................... xvi ■Chapter 1: Representing Data with Values ............................................................ 1 ■Chapter 2: Type Conversion ................................................................................ 25 ■Chapter 3: Operators ........................................................................................... 57 ■Chapter 4: Controlling Flow ................................................................................. 97 ■Chapter 5: Member Inheritance ........................................................................ 145 ■Chapter 6: Functions and Arrays ....................................................................... 181 ■Chapter 7: Traversing and Modifying the DOM Tree ......................................... 255 ■Chapter 8: Scripting CSS ................................................................................... 307 ■Chapter 9: Listening for Events ......................................................................... 347 ■Chapter 10: Scripting BOM ................................................................................ 399 ■Index ................................................................................................................. 461 iv www.allitebooks.com Contents ■Contents at a Glance ............................................................................................ iv ■About the Author ................................................................................................ xiii ■About the Technical Reviewers .......................................................................... xiv ■Acknowledgments ............................................................................................... xv ■Preface ............................................................................................................... xvi ■Chapter 1: Representing Data with Values ............................................................ 1 What Are Value Types? ................................................................................................... 1 Creating a String Literal .................................................................................................. 2 Commenting Code .................................................................................................................................. 2 Gluing Strings Together with the + Operator ......................................................................................... 3 Creating a Number Literal ............................................................................................... 4 Creating a Boolean Literal ............................................................................................... 5 Naming a Value with an Identifier ................................................................................... 6 Can I Name a Variable Anything I Want? ................................................................................................ 6 Some Valid Identifiers Are Already Taken .............................................................................................. 7 Creating an Object Literal ............................................................................................... 9 Naming Members with Identifiers ........................................................................................................ 12 Creating an Array Literal ............................................................................................... 14 Creating a Function Literal