Apress Pro JavaScript Techniques (2006).pdf

(3318 KB) Pobierz
Pro JavaScript
Techniques
John Resig
943891964.017.png 943891964.018.png 943891964.019.png 943891964.020.png 943891964.001.png 943891964.002.png 943891964.003.png 943891964.004.png 943891964.005.png 943891964.006.png
Pro JavaScript™ Techniques
Copyright © 2006 by John Resig
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-59059-727-9
ISBN-10 (pbk): 1-59059-727-3
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the
US and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book was writ-
ten without endorsement from Sun Microsystems, Inc.
Lead Editor: Chris Mills
Technical Reviewer: Dan Webb
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Dominic Shakeshaft, Jim Sumser,
Keir Thomas, Matt Wade
Project Manager: Tracy Brown Collins
Copy Edit Manager: Nicole Flores
Copy Editor: Jennifer Whipple
Assistant Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Compositor: Linda Weidemann, Wolf Creek Press
Proofreader: April Eddy
Indexer: Broccoli Information Management
Artist: April Milne
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 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 http://www.springeronline.com .
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley,
CA 94710. Phone 510-549-5930, fax 510-549-5939, e-mail info@apress.com , or visit http://www.apress.com .
The information in this book is distributed on an “as is” basis, without warranty. Although every pre-
caution 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.
The source code for this book is available to readers at http://www.apress.com in the Source Code/
Download section and on the book’s web site at http://jspro.org .
943891964.007.png 943891964.008.png
Contents at a Glance
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
PART 1 n n n Introducing Modern JavaScript
n CHAPTER 1
Modern JavaScript Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
PART 2 n n n Professional JavaScript Development
n CHAPTER 2
Object-Oriented JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
n CHAPTER 3
Creating Reusable Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
n CHAPTER 4
Tools for Debugging and Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
PART 3 n n n Unobtrusive JavaScript
n CHAPTER 5
The Document Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
n CHAPTER 6
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
n CHAPTER 7
JavaScript and CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
n CHAPTER 8
Improving Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
n CHAPTER 9
Building an Image Gallery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
PART 4 n n n Ajax
n CHAPTER 10
Introduction to Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
n CHAPTER 11
Enhancing Blogs with Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
n CHAPTER 12
Autocomplete Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
n CHAPTER 13
An Ajax Wiki . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
iii
 
943891964.009.png 943891964.010.png 943891964.011.png
PART 5 n n n The Future of JavaScript
n CHAPTER 14
Where Is JavaScript Going? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
PART 6 n n n Appendixes
n APPENDIX A
DOM Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
n APPENDIX B
Events Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325
n APPENDIX C
The Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
n INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
943891964.012.png 943891964.013.png
Contents
About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv
About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
PART 1 n n n Introducing Modern JavaScript
Modern JavaScript Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
n CHAPTER 1
Object-Oriented JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Testing Your Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Packaging for Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Unobtrusive DOM Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Document Object Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
JavaScript and CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Browser Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
PART 2 n n n Professional JavaScript
Development
Object-Oriented JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
n CHAPTER 2
Language Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Function Overloading and Type-Checking . . . . . . . . . . . . . . . . . . . . . . 21
Scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Closures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Context . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
v
 
943891964.014.png 943891964.015.png 943891964.016.png
Zgłoś jeśli naruszono regulamin