Wiley - Professional Ajax (2006).pdf

(3299 KB) Pobierz
Professional Ajax
Professional Ajax
Nicholas C. Zakas
Jeremy McPeak
Joe Fawcett
Published by Wiley Publishing, Inc.
10475 Crosspoint Boulevard Indianapolis , IN 46256 .
Copyright © 2006 by Wiley Publishing, Inc., Indianapolis, Indiana
Published simultaneously in Canada
ISBN-13: 978-0-471-77778-6
ISBN-10: 0-471-77778-1
0471777781
Manufactured in the United States of America
10 9 8 7 6 5 4 3 2 1
1B/RZ/QR/QW/IN
Library of Congress Cataloging-in-Publication Data
Zakas, Nicholas C.
Professional Ajax / Nicholas C. Zakas, Jeremy McPeak, Joe Fawcett.
p. cm.
Includes index.
ISBN-13: 978-0-471-77778-6 (paper/website)
ISBN-10: 0-471-77778-1 (paper/website)
1. JavaScript (Computer program language) 2. Asynchronous transfer mode. 3. World Wide
Web. I. McPeak, Jeremy, 1979- II. Fawcett, Joe, 1962- III. Title.
QA76.73.J39Z35 2006
005.13'3—dc22
2005034274
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any
form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise,
except as permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without
either the prior written permission of the Publisher, or authorization through payment of the
appropriate per-copy fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers,
MA 01923, (978) 750-8400, fax (978) 646-8600. Requests to the Publisher for permission
should be addressed to the Legal Department, Wiley Publishing, Inc., 10475 Crosspoint Blvd.,
Indianapolis, IN 46256, (317) 572-3447, fax (317) 572-4355, or online at
LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR
MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY
 
30122296.002.png
OR COMPLETENESS OF THE CONTENTS OF THIS WORK AND SPECIFICALLY DISCLAIM
ALL WARRANTIES, INCLUDING WITHOUT LIMITATION WARRANTIES OF FITNESS FOR A
PARTICULAR PURPOSE. NO WARRANTY MAY BE CREATED OR EXTENDED BY SALES
OR PROMOTIONAL MATERIALS. THE ADVICE AND STRATEGIES CONTAINED HEREIN
MAY NOT BE SUITABLE FOR EVERY SITUATION. THIS WORK IS SOLD WITH THE
UNDERSTANDING THAT THE PUBLISHER IS NOT ENGAGED IN RENDERING LEGAL,
ACCOUNTING, OR OTHER PROFESSIONAL SERVICES. IF PROFESSIONAL ASSISTANCE
IS REQUIRED, THE SERVICES OF A COMPETENT PROFESSIONAL PERSON SHOULD BE
SOUGHT. NEITHER THE PUBLISHER NOR THE AUTHOR SHALL BE LIABLE FOR
DAMAGES ARISING HEREFROM. THE FACT THAT AN ORGANIZATION OR WEBSITE IS
REFERRED TO IN THIS WORK AS A CITATION AND/OR A POTENTIAL SOURCE OF
FURTHER INFORMATION DOES NOT MEAN THAT THE AUTHOR OR THE PUBLISHER
ENDORSES THE INFORMATION THE ORGANIZATION OR WEBSITE MAY PROVIDE OR
RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT
INTERNET WEBSITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAPPEARED
BETWEEN WHEN THIS WORK WAS WRITTEN AND WHEN IT IS READ.
For general information on our other products and services please contact our Customer Care
Department within the United States at (800) 762-2974, outside the United States at (317) 572-
3993 or fax (317) 572-4002.
Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Programmer to Programmer, and
related trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or
its affiliates, in the United States and other countries, and may not be used without written
permission. All other trademarks are the property of their respective owners. Wiley Publishing,
Inc., is not associated with any product or vendor mentioned in this book.
Wiley also publishes its books in a variety of electronic formats. Some content that appears in
print may not be available in electronic books.
About the Authors
Nicholas C. Zakas has a BS degree in Computer Science from Merrimack College and an MBA
degree from Endicott College. He is the author of Professional JavaScript for Web Developers
as well as several online articles. Nicholas has worked in web development for more than five
years and has helped develop web solutions in use at some of the largest companies in the
world. Nicholas can be reached through his web site at www.nczonline.net .
Jeremy McPeak began tinkering with web development as a hobby in 1998. Currently working
in the IT department of a school district, Jeremy has experience developing web solutions with
JavaScript, PHP, and C#. He has written several online articles covering topics such as XSLT,
WebForms, and C#.
Joe Fawcett started programming in the 1970s and worked briefly in IT after leaving full-time
education. He then pursued a more checkered career before returning to software development
in 1994. In 2003, he was awarded the title Microsoft Most Valuable Professional in XML for
community contributions and technical expertise. Joe currently works in London as a developer
for The Financial Training Company, which provides professional certifications and business
training.
Credits
Senior Acquisitions Editor
Jim Minatel
Development Editor
John Sleeva
Technical Editor
Alexei Gorkov
Production Editor
Kathryn Duggan
Copy Editor
Michael Koch
Editorial Manager
Mary Beth Wakefield
Production Manager
Tim Tate
Vice President and Executive Group Publisher
Richard Swadley
Vice President and Executive Publisher
Joseph B. Wikert
Graphics and Production Specialists
Carrie A. Foster
Lauren Goddard
Joyce Haughey
Jennifer Heleine
Barbara Moore
Melanee Prendergast
Alicia B. South
Quality Control Technician
John Greenough
Proofreading and Indexing
TECHBOOKS Production Services
To my family and Emily, whose love and support have been invaluable over the past couple of
years.
—Nicholas C. Zakas
To my parents, Sheila and William, who instilled in me a love of reading. Thank you!
—Jeremy McPeak
Acknowledgments
It takes many people to create a book such as this, and we'd like to thank some people for their
contributions to this work.
First and foremost, thanks to everyone at Wiley for their support: Jim Minatel for starting the
process, Gabrielle Nabi for helping with the first few chapters, and John Sleeva for finishing
where Gabrielle left off. Also, a big thanks to our technical editor, Alexei Gorkov, for doing a
fantastic job of keeping us honest.
Last, a big thanks to those who provided pre-publication feedback, including Martin Honnen,
Peter Frueh, Mike Shaffer, Brad Neuberg, Steven Peterson, and Eric Miraglia.
 
Introduction
With recent advances in JavaScript, web developers have been able to create an
unprecedented user experience in web applications. Breaking free of the "click-and-wait"
paradigm that has dominated the web since its inception, developers can now bring features
formerly reserved for desktop applications onto the web using a technique called Ajax.
Ajax is an all-encompassing term surrounding the use of asynchronous HTTP requests initiated
by JavaScript for the purpose of retrieving information from the server without unloading the
page. These requests may be executed in any number of ways and using any number of
different data transmission formats. Combining this remote data retrieval with the interactivity of
the Document Object Model (DOM) has bred a new generation of web applications that seem to
defy all the traditional rules of what can happen on the web. Big companies such as Google,
Yahoo!, and Microsoft have devoted resources specifically towards the goal of creating web
applications that look and behave like desktop applications.
This book covers the various aspects of Ajax, including the different ways you can initiate HTTP
requests to the server and the different formats that can be used to carry data back and forth.
You will learn different Ajax techniques and patterns for executing client-server communication
on your web site and in web applications.
Whom This Book is For
This book is aimed at two groups of readers:
Web application developers looking to enhance the usability of their web sites and web
applications.
Intermediate JavaScript developers looking to further understand the language.
In addition, familiarity with the following related technologies is a strong indicator that this book
is for you:
XML
XSLT
Web Services
PHP
C#
HTML
CSS
This book is not aimed at beginners without a basic understanding of the aforementioned
technologies. Also, a good understanding of JavaScript is vitally important to understanding this
book. Readers who do not have this knowledge should instead refer to books such as
Beginning JavaScript, Second Edition (Wiley Publishing, ISBN 0-7645-5587-1) and Professional
JavaScript for Web Developers (Wiley Publishing, ISBN 0-7645-7908-8).
30122296.003.png
What This Book Covers
Professional Ajax provides a developer-level tutorial of Ajax techniques, patterns, and use
cases.
The book begins by exploring the roots of Ajax, covering how the evolution of the Web and new
technologies directly led to the development of Ajax techniques. A detailed discussion of how
frames, JavaScript, cookies, XML, and XMLHttp related to Ajax is included.
After this introduction, the book moves on to cover the implementation of specific Ajax
techniques. Request brokers such as hidden frames, dynamic iframes, and XMLHttp are
compared and contrasted, explaining when one method should be used over another. To make
this discussion clearer, a brief overview of HTTP requests and responses is included.
Once a basic understanding of the various request types is discussed, the book moves on to
provide indepth examples of how and when to use Ajax in a web site or web application.
Different data transmission formats—including plain text, HTML, XML, and JSON—are
discussed for their advantages and disadvantages. Also included is a discussion on web
services and how they may be used to perform Ajax techniques.
The last part of the book walks you through the creation of a full-fledged Ajax web application
called AjaxMail, which incorporates many of the techniques discussed throughout the book, and
introduces you to several Ajax libraries designed to make Ajax communication easier on
developers.
How This Book is Structured
This book begins by providing background about the origins of Ajax before moving into actual
implementation. Next, the various ways to accomplish client-server communication are
discussed, setting the stage for the rest of the book. It is recommended that you read the book
straight through, as each chapter builds on information in the previous chapters.
The chapter-level breakdown is as follows:
Chapter 1 : "What Is Ajax?" This chapter explains the origins of Ajax and the
technologies involved. It describes how Ajax developed as the Web developed and who, if
anyone, can claim ownership of the term and techniques.
Chapter 2 : "Ajax Basics." This chapter introduces the various ways to accomplish
Ajax communication, including the hidden frame technique and XMLHttp. The advantages
and disadvantages of each approach are discussed, as well as guidelines as to when each
should be used.
Chapter 3 : "Ajax Patterns." This chapter focuses on design patterns using Ajax. There
are a variety of ways to incorporate Ajax into web sites and web applications; these have
been organized into a handful of design patterns that describe best practices for Ajax
incorporation.
Chapter 4 : "XML, XPath, and XSLT." This chapter introduces XML, XPath, and XSLT
as complementary technologies to Ajax. The discussion centers on using XML as a data
transmission format and using XPath and XSLT to access and display information.
Chapter 5 : "Syndication with RSS/Atom." This chapter deals with using Ajax together
with the data syndication formats RSS and Atom to create a web-based news aggregator.
Chapter 6 : "Web Services." This chapter brings web services into the Ajax picture.
Examples of how to call web services from the client are explained, as well as how to
create server-side proxies to work around browser security restrictions.
Chapter 7 : "JSON." This chapter introduces JavaScript Object Notation (JSON) as an
alternate data transmission format for Ajax communications. Advantages and
disadvantages over using XML and plain text are discussed.
Chapter 8 : "Web Site Widgets." This chapter brings the techniques from the previous
chapters into focus by creating Ajax widgets that can be included in your web site.
Chapter 9 : "AjaxMail." This chapter walks you through the development of a complete
web application, AjaxMail. This application is an Ajax-based e-mail system that uses many
of the techniques described earlier in the book.
30122296.001.png
Zgłoś jeśli naruszono regulamin