Learn Javascript
Total Page:16
File Type:pdf, Size:1020Kb
Learn JavaScript Steve Suehring Copyright © 2012 by Steve Suehring All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means without the written permission of the publisher. ISBN: 978-0-7356-6674-0 1 2 3 4 5 6 7 8 9 LSI 7 6 5 4 3 2 Printed and bound in the United States of America. Microsoft Press books are available through booksellers and distributors worldwide. If you need support related to this book, email Microsoft Press Book Support at [email protected]. Please tell us what you think of this book at http://www.microsoft.com/learning/booksurvey. Microsoft and the trademarks listed at http://www.microsoft.com/about/legal/en/us/IntellectualProperty/ Trademarks/EN-US.aspx are trademarks of the Microsoft group of companies. All other marks are property of their respective owners. The example companies, organizations, products, domain names, email addresses, logos, people, places, and events depicted herein are fictitious. No association with any real company, organization, product, domain name, email address, logo, person, place, or event is intended or should be inferred. This book expresses the author’s views and opinions. The information contained in this book is provided without any express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will be held liable for any damages caused or alleged to be caused either directly or indirectly by this book. Acquisitions and Developmental Editor: Russell Jones Production Editor: Rachel Steely Editorial Production: Dianne Russell, Octal Publishing, Inc. Technical Reviewer: John Paul Mueller Copyeditor: Roger LeBlanc Indexer: Stephen Ingle Cover Design: Jake Rae Cover Composition: Zyg Group, LLC Illustrator: Robert Romano and Rebecca Demarest I dedicate this book to Rebecca, Jakob, and Owen —STEVE SUEHRING Contents at a Glance Introduction xiii CHAPTER 1 What Is JavaScript? 1 CHAPTER 2 JavaScript Programming Basics 23 CHAPTER 3 Building JavaScript Programs 45 CHAPTER 4 JavaScript in a Web Browser 73 CHAPTER 5 Handling Events with JavaScript 105 CHAPTER 6 Getting Data into JavaScript 133 CHAPTER 7 Styling with JavaScript 157 CHAPTER 8 Using JavaScript with Microsoft Windows 8 187 Index 207 Contents Introduction ....................................................xiii Chapter 1 What Is JavaScript? 1 A First JavaScript Program ..........................................2 Where JavaScript Fits ..............................................3 HTML, CSS, and JavaScript ....................................4 JavaScript in Windows 8 ......................................9 Placing JavaScript in a Webpage ...............................9 Writing Your First JavaScript Program ...............................11 Writing JavaScript in Visual Studio 11 .........................12 JavaScript’s Limitations. .20 Summary. .22 Chapter 2 JavaScript Programming Basics 23 JavaScript Placement: Revisited ....................................23 Basic JavaScript Syntax. .26 JavaScript Statements and Expressions ........................26 Names and Reserved Words .................................27 Spacing and Line Breaks .....................................28 Comments .................................................29 Case Sensitivity .............................................30 Operators ..................................................31 JavaScript Variables and Data Types ................................32 Variables. .32 Data Types .................................................35 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey vii Looping and Conditionals in JavaScript .............................39 Loops in JavaScript. .40 Conditionals in JavaScript ....................................41 Summary. .44 Chapter 3 Building JavaScript Programs 45 Functions .......................................................45 Function Overview ..........................................46 Function Arguments ........................................46 Calling Functions ...........................................48 Return Values ..............................................49 Function Examples ..........................................50 Scoping Revisited ...........................................54 Objects in JavaScript ..............................................56 What Does an Object Look Like? .............................56 Properties .................................................56 Methods ...................................................58 Object Enumeration .........................................61 Classes ....................................................63 Debugging JavaScript ............................................67 Debugging as a Process .....................................67 Debugging in Internet Explorer ...............................68 Summary. .71 Chapter 4 JavaScript in a Web Browser 73 JavaScript Libraries ...............................................74 Getting jQuery ...................................................74 Using a Local Copy of jQuery ................................75 Using a CDN-Hosted jQuery Library ..........................78 Testing jQuery. .79 Getting jQuery UI ................................................81 Adding jQuery UI to a Project ................................82 Testing jQuery UI ...........................................86 viii Contents The Browser Object Model. .89 Events and the window Object ...............................90 The screen Object ...........................................90 The navigator Object ........................................92 The location Object .........................................93 The DOM .......................................................95 DOM Versions ..............................................95 The DOM Tree. .96 Retrieving Elements with JavaScript and jQuery ......................98 Using jQuery, Briefly ........................................99 Retrieving Elements by ID. 100 Retrieving Elements by Class ................................102 Retrieving Elements by HTML Tag Name .....................102 Summary. .104 Chapter 5 Handling Events with JavaScript 105 Common Events with JavaScript ...................................105 Handling Mouse Events ..........................................106 Preventing the Default Action ...............................110 Attaching to an Element with On ...........................112 Validating Web Forms with jQuery ................................113 Validating on Submit .......................................113 Regular Expressions ........................................118 Finding the Selected Radio Button or Check Box. .121 Determining the Selected Drop-Down Element ...............122 The click Event Revisited ....................................125 Keyboard Events and Forms ......................................129 Summary. .131 Chapter 6 Getting Data into JavaScript 133 AJAX in Brief ...................................................133 On Servers, GETs, and POSTs ................................134 Building a Server Program ..................................135 Contents ix AJAX and JavaScript .............................................139 Retrieving Data with jQuery ......................................139 Using get() and post() .....................................140 Building an Interactive Page ................................141 Error Handling ............................................144 Using JSON for Efficient Data Exchange ............................146 Using getJSON() .......................................... 147 Sending Data to the Server .......................................148 Sending Data with getJSON ................................148 Sending Post Data .........................................153 Summary. .156 Chapter 7 Styling with JavaScript 157 Changing Styles with JavaScript ...................................157 CSS Revisited. .157 Changing CSS Properties ...................................159 Working with CSS Classes ........................................163 Determining Classes with hasClass() ......................... 163 Adding and Removing Classes ...............................164 Advanced Effects with jQuery UI ..................................167 Using Effects to Enhance a Web Application ..................167 Using jQuery UI Widgets ...................................172 Other Helpful jQuery UI Widgets ............................176 Putting It All Together: A Space Travel Demo .......................176 Summary. .186 x Contents Chapter 8 Using JavaScript with Microsoft Windows 8 187 JavaScript Is Prominent in Windows 8. .187 A Stroll Through a Windows 8 Application .........................190 Building a Windows 8 App .......................................193 Building the Application. .193 Code Analysis .............................................199 Defining a Splash Screen, Logos, and a Tile ...................202 Summary. .204 Index 207 What do you think of this book? We want to hear from you! Microsoft is interested in hearing your feedback so we can continually improve our books and learning resources for you. To participate in a brief online survey, please visit: microsoft.com/learning/booksurvey Contents xi Introduction avaScript is a popular web programming