Phil Ballard Michael Moncur
SamsTeachYourself JavaScript™ Fifth Edition
in Hours24
800 East 96th Street, Indianapolis, Indiana, 46240 USA Sams Teach Yourself JavaScript™ in 24 Hours, Fifth Edition Editor-in-Chief Mark Taub Copyright © 2013 by Pearson Education, Inc. All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, Acquisitions Editor or transmitted by any means, electronic, mechanical, photocopying, recording, or other- Mark Taber wise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has Managing Editor been taken in the preparation of this book, the publisher and author assume no responsi- Kristy Hart bility for errors or omissions. Nor is any liability assumed for damages resulting from the use of the information contained herein. Project Editor ISBN-13: 978-0-672-33608-9 Anne Goebel ISBN-10: 0-672-33608-1 Copy Editor Library of Congress Cataloging-in-Publication Data is on file. Geneil Breeze Printed in the United States of America First Printing October 2012 Indexer Erika Millen Trademarks All terms mentioned in this book that are known to be trademarks or service marks have Proofreader been appropriately capitalized. Sams Publishing cannot attest to the accuracy of this Chrissy White, information. Use of a term in this book should not be regarded as affecting the validity of Language Logistics any trademark or service mark. Publishing Coordinator Warning and Disclaimer Vanessa Evans Every effort has been made to make this book as complete and as accurate as possible, but no warranty or fitness is implied. The information provided is on an “as is” basis. The Technical Editor authors and the publisher shall have neither liability nor responsibility to any person or Joseph Greenspan entity with respect to any loss or damages arising from the information contained in this book. Cover Designer Anne Jones Bulk Sales Sams Publishing offers excellent discounts on this book when ordered in quantity for bulk Compositor purchases or special sales. For more information, please contact Nonie Ratcliff U.S. Corporate and Government Sales 1-800-382-3419 [email protected] For sales outside of the U.S., please contact International Sales [email protected] Contents at a Glance Part IV: Ajax HOUR 16 Introducing Ajax ...... 251 HOUR 17 Creating a Simple Ajax Introduction ...... 1 Library ...... 273
HOUR 18 Solving Ajax Problems ...... 287 Part I: First Steps with JavaScript
HOUR 1 Introducing JavaScript ...... 7 Part V: Using JavaScript Libraries
HOUR 2 Writing Simple Scripts ...... 21 HOUR 19 Making Life Easier with Libraries ...... 303 HOUR 3 Using Functions ...... 37 HOUR 20 A Closer Look at jQuery ...... 317 HOUR 4 Objects and Built-In Objects ...... 49 HOUR 21 The jQuery UI User Interface Library ...... 335 HOUR 5 Different Types of Data ...... 67 Part VI: Using JavaScript with Other Part II: More Advanced JavaScript Web Technologies
HOUR 6 Scripts That Do More...... 85 HOUR 22 JavaScript and Multimedia . . . . 353
HOUR 7 Object Oriented Programming. . . 103 HOUR 23 HTML5 and JavaScript ...... 365 HOUR 8 Meet JSON ...... 121 HOUR 24 JavaScript Beyond the Web Page ...... 383 HOUR 9 Responding to Events ...... 135
HOUR 10 JavaScript and Cookies ...... 155 Part VII: Appendices APPENDIX A Tools for JavaScript Part III: Working with the Document Development ...... 399 Object Model (DOM) APPENDIX B JavaScript Quick HOUR 11 Navigating the DOM...... 171 Reference ...... 403
HOUR 12 Scripting the DOM ...... 185 Index ...... 411
HOUR 13 JavaScript and CSS ...... 201
HOUR 14 Good Coding Practice ...... 217
HOUR 15 Graphics and Animation ...... 235 Table of Contents HOUR 4: DOM Objects and Built-In Objects ...... 49
Introduction ...... 1 Interacting with the User...... 49 Who This Book Is For ...... 1 Selecting Elements by Their id...... 51 The Aims of This Book ...... 1 Accessing Browser History ...... 52 Conventions Used ...... 2 Using the location Object ...... 53 Q&A, Quiz, and Exercises ...... 2 Browser Information—The navigator Object .... 54 How the Book Is Organized ...... 2 Dates and Times ...... 57 Tools You’ll Need ...... 3 Simplifying Calculation with the Math Object ...... 59 PART I: First Steps with JavaScript ...... 5 Summary ...... 64 Q&A...... 65 HOUR 1: Introducing JavaScript ...... 7 Workshop ...... 65 Web Scripting Fundamentals ...... 7 Exercises ...... 66 Server- Versus Client-Side Programming ...... 8
JavaScript in a Nutshell ...... 8 HOUR 5: Different Types of Data ...... 67 Where JavaScript Came From ...... 9 Numbers ...... 67 The
TIP Function names, like variable Calling Functions names, are case-sensitive. A Code wrapped up in a function definition will not be executed when the function called MyFunc() is dif- ferent from another called page loads. Instead, it waits quietly until the function is called. myFunc(). Also, as with variable To call a function, you simply use the function name (with the parentheses) names, it’s really helpful to the readability of your code to wherever you want to execute the statements contained in the choose meaningful function function: names. sayHello();
TIP For example, you may want to add a call to your new function sayHello() You’ve already seen numerous to the onClick event of a button: examples of using the methods associated with JavaScript objects, such as document.write() and window.alert(). Putting JavaScript Code in the Page
Methods are simply functions Up to now, our examples have all placed the JavaScript code into the that “belong” to a specific object. You learn much more part of the HTML page. Using functions lets you employ the much about objects in Hour 4, “DOM more common, and usually preferable, practice of storing our JavaScript Objects and Built-In Objects.” code in the of the page. Functions contained within aIn this listing, you can see that the function definition itself has been placed inside a