AJAX In Action [0]
Eric Pascarello
with Darren James
M A N N I N G
Ajax in Action
Licensed to jonathan zheng Licensed to jonathan zheng Ajax in Action DAVE CRANE ERIC PASCARELLO WITH DARREN JAMES M A N N I N G Greenwich (74° w. long.) Licensed to jonathan zheng For online information and ordering of this and other Manning books, please go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: orders@manning.com ©2006 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Liz Welch 209 Bruce Park Avenue Typesetter: Denis Dalinnik Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-932394-61-3 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – VHG – 09 08 07 06 05 Licensed to jonathan zheng To Hermes, Apollo, Athena, and my cats, for their wisdom —D.C. To my wife; I’m surprised you said yes —E.P. To my red-headed wife —D.J. Licensed to jonathan zheng Licensed to jonathan zheng brief contents PART 1 RETHINKING THE WEB APPLICATION ............................ 1 1 ■ A new design for the Web 3 2 ■ First steps with Ajax 31 3 ■ Introducing order to Ajax 69 PART 2 CORE TECHNIQUES ................................................... 117 4 ■ The page as an application 119 5 ■ The role of the server 159 PART 3 PROFESSIONAL AJAX ................................................. 209 6 ■ The user experience 211 7 ■ Security and Ajax 246 8 ■ Performance 279 vii Licensed to jonathan zheng viii BRIEF CONTENTS PART 4 AJAX BY EXAMPLE ...................................................... 325 9 ■ Dynamic double combo 327 10 ■ Type-ahead suggest 361 11 ■ The enhanced Ajax web portal 423 12 ■ Live search using XSLT 466 13 ■ Building stand-alone applications with Ajax 503 Licensed to jonathan zheng contents preface xix acknowledgments xxi about this book xxiv PART 1 RETHINKING THE WEB APPLICATION ........................ 1 1 A new design for the Web 3 1.1 Why Ajax rich clients? 5 Comparing the user experiences 5 ■ Network latency 9 Asynchronous interactions 12 ■ Sovereign and transient usage patterns 15 ■ Unlearning the Web 16 1.2 The four defining principles of Ajax 17 The browser hosts an application, not content 17 ■ The server delivers data, not content 19 ■ User interaction with the application can be fluid and continuous 21 ■ This is real coding and requires discipline 23 1.3 Ajax rich clients in the real world 24 Surveying the field 24 ■ Google Maps 25 1.4 Alternatives to Ajax 28 Macromedia Flash-based solutions 28 ■ Java Web Start and related technologies 28 ix Licensed to jonathan zheng x CONTENTS 1.5 Summary 29 1.6 Resources 30 2 First steps with Ajax 31 2.1 The key elements of Ajax 32 2.2 Orchestrating the user experience with JavaScript 34 2.3 Defining look and feel using CSS 36 CSS selectors 37 ■ CSS style properties 39 A simple CSS example 40 2.4 Organizing the view using the DOM 45 Working