JavaScript Visual QuickStart Guide

3y ago
38 Views
2 Downloads
7.99 MB
73 Pages
Last View : 1m ago
Last Download : 3m ago
Upload by : Grant Gall
Transcription

Final spine 1.078”full EcolorI SALU AQLU QI C K S TAT GV IVSUI CUKSTARTGRUIDE U InI DVIS U A L QU ICK S TA RT GU IDELearn JavaScript—the quick and easy way!websites to life with JavaScript and show you what to do step by step. Concise steps and explanations let you get up and running in no time. Essential reference guide keeps you coming back again and again.book will teach you all you need to know—from the basics of makingyour website interactive to adding advanced features with jQuery—and much more!download sample scripts and more.Tom Negrino and Dori Smith have demystified the web for non-geeks since 1995.Tom, a longtime contributor to Macworld magazine, has authored dozens of books.Dori cofounded the Web Standards Project, founded the Wise-Women community,and has programmed computers for decades. Together, they’ve written StylingWeb Pages with CSS: Visual QuickProject Guide and the best-selling Dreamweaver:Visual QuickStart Guide. They live in California’s wine country.www.peachpit.comFOR COMPUTERS USING:Safari 4 or later, Internet Explorer 9 or later,Firefox 3 or later, Chrome 9 or laterCAN 45.99UK 21.99LEVEL: Beginning / IntermediateMorgen Benoit PhotographyNina Susik/Shutterstock.comAUTHOR PHOTO:COVER IMAGE:9780321 9967015 3 9 9 9NEGRINOSMITHCATEGORY: JavaScript / Programming LanguagesUS 39.99ISBN-13: 978-0-321-99670-1ISBN-10:0-321-99670-4VISUAL QU IC KS TA RT G UI DE Visit the companion website at www.javascriptworld.com toPeachpit PressJavaScriptNinth EditionNinth Edition Whether you’re a JavaScript newbie or an experienced veteran, thisJavaScript Easy visual approach uses pictures to guide you through bringing yourTOM NEGRINODORI SMITHL E A R N T H E Q U I C K A N D E A S Y WAY !

V I S UA L Q U I C K S TA R T G U I D EJavaScriptNINTH EDITIONTOM NEGRINO DORI SMITHPeachpit Press

Visual QuickStart GuideJavaScript, Ninth EditionTom Negrino and Dori SmithPeachpit PressFind us on the web at www.peachpit.comTo report errors, send a note to errata@peachpit.comPeachpit Press is a division of Pearson EducationCopyright 2015 by Tom Negrino and Dori SmithProject Editor: Nancy PetersonDevelopment Editor: Scholle Sawyer McFarlandProduction Editor: Danielle FosterCopyeditor: Scout FestaIndexer: Emily GlossbrennerCover Design: RHDG / Riezebos Holzbaur Design Group, Peachpit PressInterior Design: Peachpit PressLogo Design: MINE www.minesf.comNotice of rightsAll rights reserved. No part of this book may be reproduced or transmitted in any form by any means,electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of thepublisher. For information on getting permission for reprints and excerpts, contact permissions@peachpit.com.Notice of liabilityThe information in this book is distributed on an “As is” basis, without warranty. While every precaution hasbeen taken in the preparation of the book, neither the authors nor Peachpit Press, shall have any liability to anyperson or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by theinstructions contained in this book or by the computer software and hardware products described in it.TrademarksVisual QuickStart Guide is a registered trademark of Peachpit Press, a division of Pearson Education. Many ofthe designations used by manufacturers and sellers to distinguish their products are claimed as trademarks.Where those designations appear in this book, and Peachpit Press was aware of a trademark claim, thedesignations appear as requested by the owner of the trademark. All other product names and servicesidentified throughout this book are used in editorial fashion only and for the benefit of such companies with nointention of infringement of the trademark. No such use, or the use of any trade name, is intended to conveyendorsement or other affiliation with this book.ISBN 13: 978-0-321-99670-1ISBN 10:0-321-99670-40987654321Printed in the United States of America

DedicationTo the memory of Bill Horwitz and Dorothy Negrino, because they loved learning.Special NoteWay back in 1997, when we were writing Chapter 1 of our first edition of this book, we weresearching for a way to make the concept of JavaScript objects clear, and found inspirationin the then-newest member of our family, our cat Pixel. Over the years since then, countlessreaders have told us how our “cat object” helped them to understand JavaScript better. Pixelbecame the mascot for many of our books. In the Fall of 2013, after a long and happy life,we lost him to old age. We miss him very much.Pixel, on his last day with us.

Special Thanks to:Big thanks to our editor Nancy Peterson;her expert touch, serenity, and compassionmade this edition a pleasant one to create.Extra-special thanks for her above-the-callunderstanding when we were faced with apersonal crisis.Thanks also go to our other editor, ScholleMcFarland, who stepped in and kept theproject on an even keel when Nancy wasoverscheduled.Thanks to Scout Festa for her skillful copyediting. Our heartfelt thanks to DanielleFoster, the book’s production editor, wholaid out the book and pulled off the jobwith grace and aplomb, and to the indexer,Emily Glossbrenner, who should bethanked for doing a thankless job.As always, we’re grateful to Peachpit’sNancy Ruenzel and Nancy Davis fortheir support.We’d like to express our special thanks toall of the high school, college, and university instructors who chose to use the previous editions of this book as a textbook fortheir classes.

Contents at a GlanceIntroduction . . . . . . . . . . . . . . . . . . . . . . . . xiiiChapter 1Getting Acquainted with JavaScript . . . . . . . . . . 1Chapter 2Start Me Up! . . . . . . . . . . . . . . . . . . . . . . . . 21Chapter 3Your First Web App . . . . . . . . . . . . . . . . . . . 49Chapter 4Working with Images . . . . . . . . . . . . . . . . . . . 81Chapter 5Windows and Frames . . . . . . . . . . . . . . . . . . 115Chapter 6Form Handling . . . . . . . . . . . . . . . . . . . . . . 133Chapter 7Forms and Regular Expressions . . . . . . . . . . . 171Chapter 8Handling Events . . . . . . . . . . . . . . . . . . . . . 193Chapter 9JavaScript and Cookies . . . . . . . . . . . . . . . . 219Chapter 10Objects and the DOM . . . . . . . . . . . . . . . . . 241Chapter 11Making Your Pages Dynamic . . . . . . . . . . . . . 261Chapter 12Applied JavaScript . . . . . . . . . . . . . . . . . . . 285Chapter 13Introducing Ajax . . . . . . . . . . . . . . . . . . . . . 325Chapter 14Toolkits, Frameworks, and Libraries . . . . . . . . 365Chapter 15Designing with jQuery . . . . . . . . . . . . . . . . . 385Chapter 16Building on jQuery . . . . . . . . . . . . . . . . . . . 411Chapter 17Scripting Mobile Devices . . . . . . . . . . . . . . . 425Chapter 18Bookmarklets . . . . . . . . . . . . . . . . . . . . . . . 441Appendix A JavaScript Genealogy and Reference . . . . . . . 469Appendix B JavaScript Reserved Words . . . . . . . . . . . . . . 491Appendix C Cascading Style Sheets Reference . . . . . . . . . 495Appendix D Where to Learn More . . . . . . . . . . . . . . . . . . 507Index . . . . . . . . . . . . . . . . . . . . . . . 515Contents at a Glance v

This page intentionally left blank

Table of ContentsIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . xiiiChapter 1Getting Acquainted with JavaScript . . . . . . . . . . 1What JavaScript Is . . . . . . . . . . . . . . . . . . . . . . . 2JavaScript Isn’t Java . . . . . . . . . . . . . . . . . . . . . . 3Where JavaScript Came From . . . . . . . . . . . . . . . . 5What JavaScript Can Do . . . . . . . . . . . . . . . . . . . 6What JavaScript Can’t Do . . . . . . . . . . . . . . . . . . . 7JavaScript and More . . . . . . . . . . . . . . . . . . . . . 8The Snap-Together Language . . . . . . . . . . . . . . . . 11Handling Events . . . . . . . . . . . . . . . . . . . . . . . .14Values and Variables . . . . . . . . . . . . . . . . . . . . . 15Writing JavaScript-Friendly HTML . . . . . . . . . . . . . 17What Tools to Use? . . . . . . . . . . . . . . . . . . . . . 20Chapter 2Start Me Up! . . . . . . . . . . . . . . . . . . . . . . . . . 21Where to Put Your Scripts . . . . . . . . . . . . . . . . . 23About Functions . . . . . . . . . . . . . . . . . . . . . . . 25Using External Scripts . . . . . . . . . . . . . . . . . . . . 26Putting Comments in Scripts . . . . . . . . . . . . . . . . 29Alerting the User . . . . . . . . . . . . . . . . . . . . . . . 31Confirming a User’s Choice . . . . . . . . . . . . . . . . .33Prompting the User . . . . . . . . . . . . . . . . . . . . . 35Redirecting the User with a Link . . . . . . . . . . . . . . 37Using JavaScript to Enhance Links . . . . . . . . . . . . 39Using Multi-Level Conditionals . . . . . . . . . . . . . . .43Handling Errors . . . . . . . . . . . . . . . . . . . . . . . 46Chapter 3Your First Web App . . . . . . . . . . . . . . . . . . . . 49Around and Around with Loops . . . . . . . . . . . . . . 50Passing a Value to a Function . . . . . . . . . . . . . . . 55Detecting Objects . . . . . . . . . . . . . . . . . . . . . . 57Working with Arrays . . . . . . . . . . . . . . . . . . . . . 59Working with Functions That Return Values . . . . . . . . 61Updating Arrays . . . . . . . . . . . . . . . . . . . . . . . 62Table of Contents vii

Using Do/While Loops . . . . . . . . . . . . . . . . . . .Calling Scripts Multiple Ways . . . . . . . . . . . . . . .Combining JavaScript and CSS . . . . . . . . . . . . . .Checking State . . . . . . . . . . . . . . . . . . . . . . . .Working with String Arrays . . . . . . . . . . . . . . . . .Chapter 46466687177Working with Images . . . . . . . . . . . . . . . . . . . 81Creating Rollovers . . . . . . . . . . . . . . . . . . . . . . 83Creating More Effective Rollovers . . . . . . . . . . . . . 85Building Three-State Rollovers . . . . . . . . . . . . . . . 91Triggering Rollovers from a Link . . . . . . . . . . . . . . 93Making Multiple Links Change a Single Rollover . . . . 96Working with Multiple Rollovers . . . . . . . . . . . . . . 99Creating Cycling Banners . . . . . . . . . . . . . . . . . 104Adding Links to Cycling Banners . . . . . . . . . . . . . 106Building Wraparound Slideshows . . . . . . . . . . . . . 108Displaying a Random Image . . . . . . . . . . . . . . . . .111Cycling Images with a Random Start . . . . . . . . . . . 113Chapter 5Windows and Frames . . . . . . . . . . . . . . . . . . 115Keeping a Page out of a Frame . . . . . . . . . . . . . . 117Setting a Target . . . . . . . . . . . . . . . . . . . . . . . 118Loading iframes with JavaScript . . . . . . . . . . . . . 120Working with iframes . . . . . . . . . . . . . . . . . . . . 122Creating Dynamic iframes . . . . . . . . . . . . . . . . . 124Sharing Functions Between Documents . . . . . . . . . 126Opening a New Window . . . . . . . . . . . . . . . . . . 128Loading Different Contents into a Window . . . . . . . . 131Chapter 6Form Handling . . . . . . . . . . . . . . . . . . . . . . . 133Select-and-Go Navigation . . . . . . . . . . . . . . . . . 135Changing Menus Dynamically . . . . . . . . . . . . . . . 140Making Fields Required . . . . . . . . . . . . . . . . . . 142Checking Fields Against Each Other . . . . . . . . . . . 147Identifying Problem Fields . . . . . . . . . . . . . . . . . 149Putting Form Validation into Action . . . . . . . . . . . . 151Working with Radio Buttons . . . . . . . . . . . . . . . . 156Setting One Field with Another . . . . . . . . . . . . . . 159Validating Zip Codes . . . . . . . . . . . . . . . . . . . . 162Validating Email Addresses . . . . . . . . . . . . . . . . 166viii Table of Contents

Chapter 7Forms and Regular Expressions . . . . . . . . . . . . 171Validating an Email Address withRegular Expressions . . . . . . . . . . . . . . . . . . . 173Validating a File Name . . . . . . . . . . . . . . . . . . . 178Extracting Strings . . . . . . . . . . . . . . . . . . . . . . 180Formatting Strings . . . . . . . . . . . . . . . . . . . . . 183Formatting and Sorting Strings . . . . . . . . . . . . . . 186Formatting and Validating Strings . . . . . . . . . . . . 188Replacing Elements Using Regular Expressions . . . . . 191Chapter 8Handling Events . . . . . . . . . . . . . . . . . . . . . . 193Handling Window Events . . . . . . . . . . . . . . . . . 194Mouse Event Handling . . . . . . . . . . . . . . . . . . . 201Form Event Handling . . . . . . . . . . . . . . . . . . . .209Key Event Handling . . . . . . . . . . . . . . . . . . . . . 213Advanced Event Handling . . . . . . . . . . . . . . . . . 216Chapter 9JavaScript and Cookies . . . . . . . . . . . . . . . . . 219Baking Your First Cookie . . . . . . . . . . . . . . . . . . 221Reading a Cookie . . . . . . . . . . . . . . . . . . . . . . 225Showing Your Cookies . . . . . . . . . . . . . . . . . . . 226Using Cookies as Counters . . . . . . . . . . . . . . . . 228Deleting Cookies . . . . . . . . . . . . . . . . . . . . . . 231Handling Multiple Cookies . . . . . . . . . . . . . . . . . 233Displaying “New to You” Messages . . . . . . . . . . . 235Chapter 10Objects and the DOM . . . . . . . . . . . . . . . . . . 241About Node Manipulation . . . . . . . . . . . . . . . . . 242Adding Nodes . . . . . . . . . . . . . . . . . . . . . . . . 244Deleting Nodes . . . . . . . . . . . . . . . . . . . . . . . 246Deleting Specific Nodes . . . . . . . . . . . . . . . . . . 248Inserting Nodes . . . . . . . . . . . . . . . . . . . . . . . 251Replacing Nodes . . . . . . . . . . . . . . . . . . . . . . 254Writing Code with Object Literals . . . . . . . . . . . . . 257Chapter 11Making Your Pages Dynamic . . . . . . . . . . . . . . 261Putting the Current Date into a Webpage . . . . . . . . 262Working with Days . . . . . . . . . . . . . . . . . . . . . 264Customizing a Message for the Time of Day . . . . . . . 265Table of Contents ix

Displaying Dates by Time Zone . . . . . . . . . . . . . . 266Converting 24-Hour Time to 12-Hour Time . . . . . . . 272Creating a Countdown . . . . . . . . . . . . . . . . . . . 274Hiding and Displaying Layers . . . . . . . . . . . . . . . 278Moving an Object in the Document . . . . . . . . . . . . 281Date Methods . . . . . . . . . . . . . . . . . . . . . . . . 283Chapter 12Applied JavaScript . . . . . . . . . . . . . . . . . . . . 285Using Sliding Menus . . . . . . . . . . . . . . . . . . . . 286Adding Pull-Down Menus . . . . . . . . . . . . . . . . . 289Enhancing Pull-Down Menus . . . . . . . . . . . . . . . 293A Slideshow with Captions . . . . . . . . . . . . . . . . 297A Silly Name Generator . . . . . . . . . . . . . . . . . . 301A Bar Graph Generator . . . . . . . . . . . . . . . . . . .306Style Sheet Switcher . . . . . . . . . . . . . . . . . . . . 315Chapter 13Introducing Ajax . . . . . . . . . . . . . . . . . . . . . 325Ajax: Pinning It Down . . . . . . . . . . . . . . . . . . . . 327Reading Server Data . . . . . . . . . . . . . . . . . . . . 331Parsing Server Data . . . . . . . . . . . . . . . . . . . . 339Refreshing Server Data . . . . . . . . . . . . . . . . . . 346Getting Data From a Server . . . . . . . . . . . . . . . . 349Previewing Links with Ajax . . . . . . . . . . . . . . . . . 353Auto-Completing Form Fields . . . . . . . . . . . . . . . 356Checking Whether a File Exists . . . . . . . . . . . . . . 362Chapter 14Toolkits, Frameworks, and Libraries . . . . . . . . . 365Adding jQuery . . . . . . . . . . . . . . . . . . . . . . . . 367Updating a Page with jQuery . . . . . . . . . . . . . . . 370Interacting with jQuery . . . . . . . . . . . . . . . . . . . 371Interacting and Updating . . . . . . . . . . . . . . . . . 374Striping Tables . . . . . . . . . . . . . . . . . . . . . . . 376Sorting Tables . . . . . . . . . . . . . . . . . . . . . . . .380Chapter 15Designing with jQuery . . . . . . . . . . . . . . . . . 385Highlighting New Elements . . . . . . . . . . . . . . . . 386Creating Accordion Menus . . . . . . . . . . . . . . . . 389Creating Smarter Dialogs . . . . . . . . . . . . . . . . . 392x Table of Contents

Auto-Completing Fields . . . . . . . . . . . . . . . . . . 396Adding Sortable Tabs . . . . . . . . . . . . . . . . . . . 398Using Check Boxes as Buttons . . . . . . . . . . . . . . 401Adding a Calendar to Your Page . . . . . . . . . . . . . 404Using ThemeRoller to Customize Your Look . . . . . . .409Chapter 16Building on jQuery . . . . . . . . . . . . . . . . . . . . 411Using jQuery as a Foundation . . . . . . . . . . . . . . . 412Dragging and Dropping Elements . . . . . . . . . . . . 414Using jQuery with External Data . . . . . . . . . . . . . 417Using jQuery Plugins . . . . . . . . . . . . . . . . . . . 420Adding a jQuery Audio Plugin . . . . . . . . . . . . . . . 423Chapter 17Scripting Mobile Devices . . . . . . . . . . . . . . . . 425Changing Your Orientation . . . . . . . . . . . . . . . . 426Handling Touch Events . . . . . . . . . . . . . . . . . . . 433Differentiating Devices . . . . . . . . . . . . . . . . . . . 436Locating Your Device . . . . . . . . . . . . . . . . . . . . 438Chapter 18Bookmar

Web Pages with CSS: Visual QuickProject Guide and the best-selling Dreamweaver: Visual QuickStart Guide. They live in California’s wine country. Easy visual approach uses pictures to guide you through bringing your websites to life with JavaScript and show you what to do step by step.

Related Documents:

JavaScript Manual for LCCS Teachers 13 Client-side JavaScript vs. server-side JavaScript For many years JavaScript was a client-side scripting language. This was because JavaScript programs could only be run from inside web browsers which were installed on client machines. Because of the fact that JavaScript code can run on client devices it means

- The Spark web app framework . Yahoo JavaScript PHP Amazon.com JavaScript Java, C , Perl Wikipedia.org JavaScript PHP, Hack Twitter.com JavaScript C , Java, Scala, Ruby Bing JavaScript ASP.net eBay.com JavaScript Java, JavaScript, Scala . Note the MVC architecture

JavaScript. Check a framework's documentation for details. Using the SDK with Web Browsers All major web browsers support execution of JavaScript. JavaScript code that is running in a web browser is often called client-side JavaScript. Using the SDK for JavaScript in a web browser differs from the way in which you use it for Node.js. The

Praise for Effective JavaScript "Living up to the expectation of an Effective Software Development Series pro-gramming book, Effective JavaScript by Dave Herman is a must-read for anyone who wants to do serious JavaScript programming. The book provides detailed explanations of the inner workings of JavaScript, which helps readers take better

JavaScript directly within the HTML file. We can also include more than one script tag and can include a mix of internal JavaScript and external JavaScript files. This will be useful, for example, if we have some JavaScript that is appropriate for many of our webpages and then some JavaScript that is specific to one page. Function Definition

Windows 8, upgraded the JavaScript language to a first class member in their Visual Studio development suite [20]. Supporting the claim that JavaScript will be the dominant language of future web applications. With these rising demands to run large JavaScript applications, there is a need for faster JavaScript execution.

AWS SDK for JavaScript Developer Guide for SDK Version 3 Maintenance and support for SDK major versions What is the AWS SDK for JavaScript? Welcome to the AWS SDK for JavaScript Developer Guide. This guide provides general information about setting up and configuring the AWS SDK for JavaScript. It also walks you through examples and tutorial

not know; am I my brother’s keeper?’ (Genesis 4:9) N NOVEMBER 2014 the Obama administration in the United States announced an extension of relief for immigrant families, prompting one cartoonist to caricature ‘an immigrant family climbing through a window to crash a white family’s Thanksgiving dinner’ with the ‘white father unhappily telling his family, “Thanks to the president .