Ajax Programming for the Absolute Beginner
Total Page:16
File Type:pdf, Size:1020Kb
Ajax Programming for the Absolute Beginner Jerry Lee Ford, Jr. Course Technology PTR A part of Cengage Learning Australia, Brazil, Japan, Korea, Mexico, Singapore, Spain, United Kingdom, United States Ajax Programming for the Absolute © 2009 Course Technology, a part of Cengage Learning. Beginner: Jerry Lee Ford, Jr. ALL RIGHTS RESERVED. No part of this work covered by the copyright Publisher and General Manager, Course herein may be reproduced, transmitted, stored, or used in any form or by Technology PTR: Stacy L. Hiquet any means graphic, electronic, or mechanical, including but not limited to photocopying, recording, scanning, digitizing, taping, Web distribution, Associate Director of Marketing: information networks, or information storage and retrieval systems, except Sarah Panella as permitted under Section 107 or 108 of the 1976 United States Copyright Manager of Editorial Services: Act, without the prior written permission of the publisher. Heather Talbot Marketing Manager: Mark Hughes For product information and technology assistance, contact us at Cengage Learning Customer & Sales Support, 1-800-354-9706 Acquisitions Editor: Mitzi Koontz For permission to use material from this text or product, submit all Project Editor: Jenny Davidson requests online at cengage.com/permissions Further permissions questions can be emailed to [email protected] Technical Reviewer: Keith Davenport PTR Editorial Services Coordinator: Jen Blaney All trademarks are the property of their respective owners. Library of Congress Control Number: 2008928834 Interior Layout Tech: Value-Chain ISBN-13: 978-1-59863-564-5 Cover Designer: Mike Tanamachi ISBN-10: 1-59863-564-6 Indexer: Katherine Stimson eISBN-10: 1-43545-531-2 Course Technology Proofreader: Sara Gullion 25 Thomson Place Boston, MA 02210 USA Cengage Learning is a leading provider of customized learning solutions with office locations around the globe, including Singapore, the United Kingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at: international.cengage.com/region Cengage Learning products are represented in Canada by Nelson Education, Ltd. For your lifelong learning solutions, visit courseptr.com Visit our corporate website at cengage.com Printed in Canada 1 2 3 4 5 6 7 11 10 09 To my mother and father for always being there, and to my wonderful children, Alexander, William, and Molly, and my beautiful wife, Mary. ACKNOWLEDGMENTS here are several individuals to whom I owe many thanks for their help and Tassistance in the development of this book. I’ll start by thanking Mitzi Koontz, who served as the book’s acquisitions editor. Special thanks also go out to Jenny Davidson for serving as the book's project editor. I also want to thank Keith Davenport for all the valuable input and advice. In addition, I would like to thank everyone else at Course Technology PTR for all their hard work. ABOUT THE AUTHOR erry Lee Ford, Jr. is an author, educator, and an IT professional with over J18 years of experience in information technology, including roles as an automation analyst, technical manager, technical support analyst, au- tomation engineer, and security analyst. He is the author of 28 books and co-author of two additional books. His published works include AppleScript Studio Program- ming for the Absolute Beginner, Microsoft Windows PowerShell Programming for the Absolute Beginner, Microsoft Visual Basic 2005 Express Edition Programming for the Abso- lute Beginner, Microsoft VBScript Professional Projects, Microsoft Windows Shell Scripting and WSH Administrator’s Guide, Microsoft Windows Shell Script Programming for the Ab- solute Beginner, Learn JavaScript in a Weekend, Second Edition, and Microsoft Windows XP Professional Administrator’s Guide. Jerry has a master’s degree in business adminis- tration from Virginia Commonwealth University in Richmond, Virginia, and he has over five years of experience as an adjunct instructor teaching networking courses in information technology. This page intentionally left blank Table of Contents Introduction.......................................................... xiv Part I Introducing Ajax....................................................... 1 Chapter 1 An Ajax Overview...................................................... 3 Project Preview: The Joke of the Day Application......................................................... 4 Introducing Ajax.................................................................................................................... 5 Ajax Technologies ......................................................................................................... 6 Traditional Web Development Versus Ajax Development................................. 6 Examples of Real-World Ajax Applications and Websites.......................................... 9 Search Engine Makeovers............................................................................................ 9 Suggest Styled Applications ..................................................................................... 10 Google Maps.................................................................................................................. 13 Netflix............................................................................................................................. 14 Virtual Desktop Applications................................................................................... 14 Photo Management Using Flickr............................................................................. 16 Ajax Instant Message Applications......................................................................... 16 Online Calendars......................................................................................................... 17 Back to the Joke of the Day Application........................................................................ 18 Designing the Application........................................................................................ 18 The Final Result ........................................................................................................... 25 Summary................................................................................................................................ 26 Part II Learning JavaScript and the DOM....................... 27 Chapter 2 An Introduction to JavaScript............................. 29 Project Preview: The Number Guessing Game............................................................. 29 JavaScript—Ajax’s Programming Language.................................................................. 31 A Little JavaScript Background Information........................................................ 31 Browser Compatibility Issues................................................................................... 32 viii Ajax Programming for the Absolute Beginner Working with JavaScript.................................................................................................... 33 Creating a Simple JavaScript.................................................................................... 34 Executing Your JavaScript......................................................................................... 34 Four Ways of Working with JavaScript.......................................................................... 35 Embedding JavaScripts in the HEAD Section....................................................... 35 Embedding JavaScripts in the BODY Section....................................................... 37 Placing JavaScripts in External Files ...................................................................... 38 Embedding JavaScripts in HTML Tags.................................................................... 38 Understanding JavaScript Statement Syntax ...................................................... 39 Dealing with Browsers That Do Not Support JavaScript .................................. 39 Documenting Your Scripts Using Comments...................................................... 40 Working with Different Types of Values....................................................................... 41 Creating JavaScript Variables................................................................................... 41 Assigning Variable Names ........................................................................................ 42 Understanding Variable Scope ................................................................................ 42 Working with Local Variables.................................................................................. 42 Doing a Little Math..................................................................................................... 43 Assigning and Modifying Variable Values............................................................ 44 Applying Conditional Logic.............................................................................................. 47 Introducing the if Statement................................................................................... 47 Multi-line if Statements............................................................................................. 48 Providing for Alternative Conditions .................................................................... 48 Nesting if Statements................................................................................................. 49 Working with the switch Statement.....................................................................