Ajax Programming For The Absolute Beginner - Projanco

Transcription

Ajax Programmingfor the AbsoluteBeginnerJerry Lee Ford, Jr.Course Technology PTRA part of Cengage LearningAustralia, Brazil, Japan, Korea, Mexico, Singapore, Spain, United Kingdom, United States

Ajax Programming for the AbsoluteBeginner: Jerry Lee Ford, Jr.Publisher and General Manager, CourseTechnology PTR: Stacy L. HiquetAssociate Director of Marketing:Sarah PanellaManager of Editorial Services:Heather TalbotMarketing Manager: Mark Hughes 2009 Course Technology, a part of Cengage Learning.ALL RIGHTS RESERVED. No part of this work covered by the copyrightherein may be reproduced, transmitted, stored, or used in any form or byany means graphic, electronic, or mechanical, including but not limited tophotocopying, recording, scanning, digitizing, taping, Web distribution,information networks, or information storage and retrieval systems, exceptas permitted under Section 107 or 108 of the 1976 United States CopyrightAct, without the prior written permission of the publisher.For product information and technology assistance, contact us atCengage Learning Customer & Sales Support, 1-800-354-9706Acquisitions Editor: Mitzi KoontzProject Editor: Jenny DavidsonTechnical Reviewer: Keith DavenportPTR Editorial Services Coordinator:Jen BlaneyInterior Layout Tech: Value-ChainCover Designer: Mike TanamachiIndexer: Katherine StimsonProofreader: Sara GullionFor permission to use material from this text or product, submit allrequests online at cengage.com/permissions Further permissionsquestions can be emailed to permissionrequest@cengage.comAll trademarks are the property of their respective owners.Library of Congress Control Number: 2008928834ISBN-13: 978-1-59863-564-5ISBN-10: 1-59863-564-6eISBN-10: 1-43545-531-2Course Technology25 Thomson PlaceBoston, MA 02210USACengage Learning is a leading provider of customized learning solutionswith office locations around the globe, including Singapore, the UnitedKingdom, Australia, Mexico, Brazil, and Japan. Locate your local office at:international.cengage.com/regionCengage Learning products are represented in Canada byNelson Education, Ltd.For your lifelong learning solutions, visit courseptr.comVisit our corporate website at cengage.comPrinted in Canada1 2 3 4 5 6 7 11 10 09

To my mother and father for always being there, and to mywonderful children, Alexander, William, and Molly, andmy beautiful wife, Mary.

ACKNOWLEDGMENTShere are several individuals to whom I owe many thanks for their help andassistance in the development of this book. I’ll start by thankingMitzi Koontz, who served as the book’s acquisitions editor. Special thanksalso go out to Jenny Davidson for serving as the book's project editor. I also wantto thank Keith Davenport for all the valuable input and advice. In addition, I wouldlike to thank everyone else at Course Technology PTR for all their hard work.T

ABOUT THE AUTHORerry Lee Ford, Jr. is an author, educator, and an IT professional with over18 years of experience in information technology, including roles as anautomation analyst, technical manager, technical support analyst, automation engineer, and security analyst. He is the author of 28 books and co-authorof two additional books. His published works include AppleScript Studio Programming for the Absolute Beginner, Microsoft Windows PowerShell Programming for theAbsolute Beginner, Microsoft Visual Basic 2005 Express Edition Programming for the Absolute Beginner, Microsoft VBScript Professional Projects, Microsoft Windows Shell Scriptingand WSH Administrator’s Guide, Microsoft Windows Shell Script Programming for the Absolute Beginner, Learn JavaScript in a Weekend, Second Edition, and Microsoft Windows XPProfessional Administrator’s Guide. Jerry has a master’s degree in business administration from Virginia Commonwealth University in Richmond, Virginia, and hehas over five years of experience as an adjunct instructor teaching networkingcourses in information technology.J

This page intentionally left blank

Table of ContentsIntroduction. xivPart IIntroducing Ajax. 1Chapter 1An Ajax Overview. 3Project Preview: The Joke of the Day Application. 4Introducing Ajax. 5Ajax Technologies . 6Traditional Web Development Versus Ajax Development. 6Examples of Real-World Ajax Applications and Websites. 9Search Engine Makeovers. 9Suggest Styled Applications . 10Google Maps. 13Netflix. 14Virtual Desktop Applications. 14Photo Management Using Flickr. 16Ajax Instant Message Applications . 16Online Calendars. 17Back to the Joke of the Day Application. 18Designing the Application . 18The Final Result . 25Summary. 26Part IILearning JavaScript and the DOM. 27Chapter 2An Introduction to JavaScript. 29Project Preview: The Number Guessing Game. 29JavaScript—Ajax’s Programming Language. 31A Little JavaScript Background Information. 31Browser Compatibility Issues. 32

viiiAjax Programming for the Absolute BeginnerWorking with JavaScript. 33Creating a Simple JavaScript. 34Executing Your JavaScript. 34Four Ways of Working with JavaScript. 35Embedding JavaScripts in the HEAD Section. 35Embedding JavaScripts in the BODY Section . 37Placing JavaScripts in External Files . 38Embedding JavaScripts in HTML Tags. 38Understanding JavaScript Statement Syntax . 39Dealing with Browsers That Do Not Support JavaScript . 39Documenting Your Scripts Using Comments. 40Working with Different Types of Values. 41Creating JavaScript Variables. 41Assigning Variable Names . 42Understanding Variable Scope . 42Working with Local Variables. 42Doing a Little Math . 43Assigning and Modifying Variable Values . 44Applying Conditional Logic. 47Introducing the if Statement . 47Multi-line if Statements. 48Providing for Alternative Conditions . 48Nesting if Statements. 49Working with the switch Statement. 50Working Efficiently with Loops. 52Working with the for Statement . 53Working with the while Statement . 53Working with the do. . .while Statement. 55Altering Loop Execution. 56Skipping Loop Iterations . 57Back to the Number Guessing Game. 58Designing the Application . 58Summary. 62Chapter 3A Deeper Dive into JavaScript. 65Project Preview: The Rock, Paper, Scissors Game. 66Improving JavaScript Organization with Functions. 67Organizing Code Statements into Functions . 67Controlling Function Execution . 68Developing Applications That Respond to Events. 71Developing Event-Driven Scripts. 71Working with Different JavaScript Events . 72Processing Forms . 75

ContentsixDiv and Span Tags. 77Working with the DIV /DIV Tags. 77Working with the SPAN /SPAN Tags . 77Programmatically Replacing Text without Screen Refresh . 78Managing Collections of Data. 80Accessing Individual Array Elements . 81Using Loops to Process Arrays. 82Sorting the Contents of Arrays . 83Back to the Rock, Paper, Scissors Application. 84Designing the Application . 84Summary. 89Chapter 4Understanding the Document Object Model. 91Project Preview: The Ajax Story of the Day Application. 92An Introduction to the Document Object Model. 93The DOM Tree. 94Walking the DOM Tree. 96Accessing DOM Elements by ID . 97Accessing DOM Elements Using DOM Properties . 98A Mixed Navigation Approach . 100Dynamically Updating Web Page Content. 101Back to the Ajax Story of the Day Application. 103Designing the Application . 104Summary. 109Part IIIBuilding Ajax Applications. 111Chapter 5Ajax Basics. 113Project Preview: The Ajax Typing Challenge. 114Connecting Your Applications to Web Servers. 115XMLHttpRequest Methods . 116XMLHttpRequest Properties . 116Working with the XMLHttpRequest Object. 117Instantiating the XMLHttpRequest Object . 117Opening a New Connection. 118Waiting for the Web Server’s Response . 120Handling the Web Server Response . 121Wrapping Things Up . 122Putting All the Pieces Together to Create a Working Ajax Application. 122Managing Concurrent XMLHttpRequests. 124Using Ajax to Set Up Mouseovers. 125

xAjax Programming for the Absolute BeginnerLeveraging Ajax Frameworks. 127Popular Ajax Frameworks . 128Framework Demo—Using the CBA Framework . 130Back to the Ajax Typing Challenge Application. 132Designing the Application . 132The Final Result . 137Summary. 138Chapter 6Digging Deeper into Ajax. 139Project Preview: The Ajax Google Suggest Application. 140Using Ajax to Manipulate Graphics. 141Sending Data to Web Servers. 144A Quick Example of How to Work with PHP . 145Sending Data to Web Servers for Processing . 147Executing Server-Supplied JavaScript. 153Back to the Ajax Google Suggest Application. 155Designing the Application . 156The Final Result . 163Summary. 163Part IVData Management and Presentation. 165Chapter 7Working with XML. 167Project Preview: The Who Am I? Application. 168An Introduction to XML. 169Rules for Formulating XML Tags . 170XML Element Syntax. 171Including the XML Declaration Instruction. 171Commenting Your XML Files. 172Working with Elements with No Content. 172Understanding the Types of Elements in Use. 173Verifying That Your XML Files Are Well-Formed. 174Understanding XML Trees. 175A Depiction of a Small XML File . 175JavaScript Properties That Work with XML Trees. 176Navigating XML Files. 176Eliminating White Space . 179Processing XML Element Attributes. 181JSON: JavaScript Object Notation—An Alternative to XML. 184Back to the Who Am I? Application. 184Designing the Application . 185

ContentsxiThe Final Result . 193Summary. 193Chapter 8Working with Cascading Style Sheets. 195Project Preview: The Fortune Telling Game. 195An Introduction to CSS. 197CSS Syntax . 198Using CSS to Specify Style, Color, and Presentation. 198Controlling Font Presentation. 199Managing the Display of Text . 200Controlling Color and Background . 201Exercising Control over Content Location . 202Adding CSS to Your HTML Pages. 205Using Inline Styles . 206Defining Embedded Style Elements. 206Working with External Style Sheets . 208Back to the Fortune Telling Game. 209Designing the Application . 209The Final Result . 220Summary. 220Chapter 9Working with Ajax and PHP. 223Project Preview: Scramble—The Word Guessing Game. 223Introduction to PHP. 225The Basics of Working with PHP. 226Embedding PHP into Your HTML Pages. 226Writing Stand-alone PHP Scripts. 227PHP Coding. 228Returning Data Back to Your Ajax Application. 229Commenting Your PHP Code . 230Storing Data in Variables . 231Managing Collections of Data Using Arrays . 231Data Assignments. 232Performing Mathematic Calculations . 232Comparing Values. 233Performing Conditional Logic . 233Working with Loops . 235Working with Functions. 239Creating and Executing Custom Functions . 239Taking Advantage of Built-in PHP Functions. 240Processing Application Input. 240Retrieving Arguments Passed Using the GET Option . 240

xiiAjax Programming for the Absolute BeginnerRetrieving Arguments Passed Using the Post Option. 241Storing and Accessing Data. 241Creating and Accessing Files . 242Writing to Files . 243Reading from Files . 244A Few Words About Working with Databases. 245Back to Scramble—The Word Guessing Game. 245Designing the Application . 245The Final Result . 250Summary. 250Chapter 10Important Ajax Design Issues. 253Programming Hurdles That All Ajax Developers Face. 253Recognize That Not All Browsers Support JavaScript. 254Do Not Let Ajax Alienate Your Users. 255Ajax Applications Disable the Browser’s Back and Forward Buttons . 256Don’t Make Unexpected Changes . 257Ajax Applications Are Not Easily Bookmarked . 257Ajax Applications Pose Problems for Search Engines. 257Dynamic Updates Are Not Always Easily Noticed . 258Data Exchange Behind the Scenes May Make Users Uncomfortable. 258Ajax Applications Do Not Run on a Single Platform . 259Don’t Build Slow Ajax Applications . 259Ajax Applications May Create New Security Concerns . 260Don’t Overuse Ajax. 261Follow Good Development Practices. 261Summary. 262Appendix AWhat’s on the Companion Website?. 263Downloading the Book’s Source Code. 264Appendix BWhat Next?. 265HTML Resources. 266Wikipedia’s HTML Page . 266WC3’s HTML 4.01 Specification Page . 266HTML.net’s Free HTML Tutorial . 267The HTML Document Object Model. 268Wikipedia’s Document Object Model Page . 268W3C’s Document Object Model (DOM) Page. 269HTML DOM Tutorial. 269

ContentsxiiiXMLHttpRequest Resources. 270Wikipedia’s XMLHttpRequest Page . 270W3C’s XMLHttpRequest Object Page. 271XMLHttpRequest Tutorial . 272Resources for Cascading Style Sheets. 272Wikipedia’s Cascading Style Sheets Page . 273WC3’s Cascading Style Sheets Page . 273CSS Tutorial Page. 274JavaScript Resources. 275Wikipedia’s JavaScript Page . 275JavaScript.com. 276JavaScript Tutorial . 276XML Resources. 277Wikipedia’s XML Page . 277W3C’s Extensible Markup Language (XML) Page . 278XML Tu

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-