Xsl:Variable
Total Page:16
File Type:pdf, Size:1020Kb
01_106754 ffirs.qxp 2/9/07 6:13 PM Page iii Beginning Ajax Chris Ullman Lucinda Dykes 01_106754 ffirs.qxp 2/9/07 6:13 PM Page ii 01_106754 ffirs.qxp 2/9/07 6:13 PM Page i Beginning Ajax 01_106754 ffirs.qxp 2/9/07 6:13 PM Page ii 01_106754 ffirs.qxp 2/9/07 6:13 PM Page iii Beginning Ajax Chris Ullman Lucinda Dykes 01_106754 ffirs.qxp 2/9/07 6:13 PM Page iv Beginning Ajax Published by Wiley Publishing, Inc. 10475 Crosspoint Boulevard Indianapolis, IN 46256 www.wiley.com Copyright © 2007 by Wiley Publishing, Inc., Indianapolis, Indiana Published simultaneously in Canada ISBN: 978-0-470-10675-4 Manufactured in the United States of America 10 9 8 7 6 5 4 3 2 1 Library of Congress Cataloging-in-Publication data is available from the publisher. 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 Sec- tions 107 or 108 of the 1976 United States Copyright Act, without either the prior written permission of the Pub- lisher, 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 permis- sion 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 http://www.wiley.com/go/permissions. LIMIT OF LIABILITY/DISCLAIMER OF WARRANTY: THE PUBLISHER AND THE AUTHOR MAKE NO REPRESENTATIONS OR WARRANTIES WITH RESPECT TO THE ACCURACY 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 HERE- FROM. THE FACT THAT AN ORGANIZATION OR WEB SITE 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 WEB SITE MAY PROVIDE OR RECOMMENDATIONS IT MAY MAKE. FURTHER, READERS SHOULD BE AWARE THAT INTERNET WEB SITES LISTED IN THIS WORK MAY HAVE CHANGED OR DISAP- PEARED 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. 01_106754 ffirs.qxp 2/9/07 6:13 PM Page v About the Authors Chris Ullman is a freelance Web developer and technical author who has spent many years working with ASP/ASP.NET. Coming from a computer science background, he started initially as a UNIX/Linux guru, who gravitated toward Microsoft technologies during the summer of ASP (1997). He cut his teeth on Wrox Press ASP guides, and since then he has written and contributed to more than 25 books, most notably as lead author for Wrox’s bestselling Beginning ASP/ASP.NET 1.x series. He has contributed chapters to books on PHP, ColdFusion, JavaScript, Web Services, C#, XML, and other Internet-related technologies. After he left Wrox as a full-time employee in August 2001, he branched out into VB.NET/C# programming and ASP.NET development, and he started his own business, CUASP Consulting Ltd, in April 2003. He maintains a variety of Web sites from http://www.cuasp.co.co.uk (his “work” site) to http://www.atomicwise.com (a selection of his writings on music and art). He now divides his time between his family and composing electronic sounds for his music project, Open E. Lucinda Dykes is a freelance Web developer, teacher, and technical author who has been writing code and developing Web sites since 1994. She started her career in a high-tech area of medicine but left medicine to pursue her interests in technology and the Web. She has been involved in client-side devel- opment with JavaScript for many years through her company, Zero G Web, and teaches JavaScript courses online for an international group of students at eclasses.org. She has also authored numerous technical books about XML, XHTML, and Web application development with Dreamweaver. 01_106754 ffirs.qxp 2/9/07 6:13 PM Page vi 01_106754 ffirs.qxp 2/9/07 6:13 PM Page vii Credits Senior Acquisitions Editor Graphics and Production Specialists Jim Minatel Jennifer Mayberry Alicia B. South Development Editor Kevin Shafer Quality Control Technicians Laura Albert Technical Editors John Greenough Alexei Gorkov Melanie Hoffman Sam Judson Jessica Kramer Todd Meister Matthew Carl Tyson Project Coordinator Spike Xavier Jennifer Theriot Production Editor Proofreading and Indexing Rachel Meyers Aptara Copy Editor Anniversary Logo Design Nancy Kruse Hannigan Richard Pacifico 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 01_106754 ffirs.qxp 2/9/07 6:13 PM Page viii All my love to my wife, Kate, and the boys. —Chris Ullman With love and thanks to Wali for putting up with being married to a writer; to my mother, Doris Dykes, for instilling a lifelong love of reading and books; and to all the Web pioneers and innovators who shared their knowledge with the rest of us. —Lucinda Dykes viii 01_106754 ffirs.qxp 2/9/07 6:13 PM Page ix Acknowledgments Thanks to my fellow author, Lucinda, for being available for queries and comments. Thanks to Kevin Shafer for his tremendously measured and consistent input on my text. Thanks to Alexei Gorkov for his excellent and incisive input on the chapters and code. And thanks to Jim Minatel for being there to field my numerous requests. —Chris Ullman Thanks to my fellow author Chris for everything I’ve learned about server-side development from him. Thanks to Kevin Shafer for tolerating all the quirks of working with two authors long distance. Thanks to Alexei Gorkov for an outstanding technical review and suggestions about the code. And thanks to Jim Minatel for always being there to answer our questions, for providing excellent suggestions, and for giv- ing me the opportunity to be involved in this project. —Lucinda Dykes 01_106754 ffirs.qxp 2/9/07 6:13 PM Page x 02_106754 ftoc.qxp 2/9/07 6:14 PM Page xi Contents Introduction xxi Chapter 1: Introducing Ajax 1 What Is Ajax? 2 Ajax in Action 3 flickr 3 Basecamp 4 Amazon (A9.com) 5 Google Suggest and Google Maps 6 Other Sites 7 Bad Examples 8 Ajax: The Acronym 9 XHTML and CSS 9 The Document Object Model (DOM) 10 JavaScript 11 XML, XSLT, and XPath 12 The XMLHttpRequest Object 14 Server-Side Technologies 15 The Ajax Application Model 16 Why Should I Use Ajax? 17 Partial Page Updating 17 Invisible Data Retrieval 18 Constant Updating 18 Smooth Interfaces 18 Simplicity and Rich Functionality 18 Drag and Drop 18 When Not to Use Ajax 18 Poor Responsiveness 19 Breaks the Back Button on Your Browser 19 Breaking Bookmarks and Blocking Search Engine Indexes 19 Strain on the Browser 19 Who Can/Can’t Use Ajax? 20 Create Your Own Example 20 Summary 29 Exercises 30 02_106754 ftoc.qxp 2/9/07 6:14 PM Page xii Contents Chapter 2: JavaScript Refresher 31 Core JavaScript 32 Syntax 32 Variables 32 Primitive Datatypes 33 Reference Datatypes 33 Operators 34 Assignment Operator 34 Arithmetic Operators 34 Comparison Operators 35 Logical Operators 35 Increment and Decrement Operators 36 Statements 37 Conditional Statements 37 Loops 38 Functions 40 Object-Oriented JavaScript 41 Built-in Objects 41 Browser Objects 42 User-Defined Objects 43 Constructors 43 Prototypes 44 Destroying Objects 45 The Document Object Model 47 The Document as a Family Tree 48 The Document as a Node Tree 48 DOM Methods for Accessing Objects 49 getElementById 49 getElementsByTagName 50 Creating Nodes 51 The innerHTML Alternative 53 JavaScript and Events 54 Event Models 54 Event Registration 55 The Internet Explorer Event Registration Model 56 The W3C DOM Event Registration Model 56 Event Objects 57 Summary 60 Exercises 61 xii 02_106754 ftoc.qxp 2/9/07 6:14 PM Page xiii Contents Chapter 3: Ajax and Server-Side Technologies 63 Ajax and the Server-Side Technologies 64 Forms and HTML Controls 64 The Forms Model of Submission 64 The Ajax/JavaScript Model of Submission 65 From the Server Side Onward 66 Submitting Data to the Server 67 The Server Receives the Request 67 Writing the HTTP Response 68 The XMLHttpRequest Object 69 The Callback Function 69 The responseText Property 70 The responseXML