TEAM LinG JAVASCRIPT DEMYSTIFIED This page intentionally left blank. JAVASCRIPT DEMYSTIFIED JIM KEOGH McGraw-Hill/Osborne New York Chicago San Francisco Lisbon London Madrid Mexico City Milan New Delhi San Juan Seoul Singapore Sydney Toronto Copyright © 2005 by The McGraw-Hill Companies. All rights reserved. Manufactured in the United States of America. Except as permitted under the United States Copyright Act of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval system, without the prior written permission of the publisher. The material in this eBook also appears in the print version of this title: 0-07-226134-X. All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trademarked name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infringement of the trademark. Where such designations appear in this book, they have been printed with initial caps. McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate training programs. For more information, please contact George Hoare, Special Sales, at george_hoare@mcgraw-hill.com or (212) 904-4069. TERMS OF USE This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw- Hill’s prior consent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your right to use the work may be terminated if you fail to comply with these terms. THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy, error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work, even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause whatsoever whether such claim or cause arises in contract, tort or otherwise. DOI: 10.1036/007226134X 0071471391 This book is dedicated to Anne, Sandy, Joanne, Amber-Leigh Christine, and Graff, without whose help and support this book couldn’t have been written. ABOUT THE AUTHOR Jim Keogh is on the faculty of Columbia University and Saint Peter’s College in Jersey City, New Jersey. He developed the e-commerce track at Columbia Univer- sity. Keogh has spent decades developing applications for major Wall Street corpo- rations and is the author of more than 60 books, including J2EE: The Complete Reference, Java Demystifi ed, ASP.NET Demystifi ed, Data Structures Demystifi ed, XML Demystifi ed, and others in the Demystifi ed series. vii CONTENTS Introduction xv CHAPTER 1 An Inside Look at JavaScript 1 Answers to Common Questions About JavaScript 2 JavaScript: A Limited-Featured Programming Language 3 Getting Down to JavaScript 4 Object Name 4 Property 5 Methods 5 The Dot Syntax 6 The Main Event 6 Writing Your First JavaScript 7 “Old Timers” Don’t Like JavaScript 10 Spicing Up Your JavaScript 11 Looking Ahead 12 Quiz 12 CHAPTER 2 Variables, Operators, and Expressions 15 Values and Variables 16 Values 16 Variables 17 For more information about this title, click here viii JavaScript Demystifi ed Operators and Expressions 24 Parts of an Expression 24 Multiple Operations 24 Types of Operators 25 Looking Ahead 40 Quiz 40 CHAPTER 3 Condition Statements 43 if Statement 45 The if Statement in Action 45 The if else Statement 46 The if else if Statement 48 Other Variations of the if Statement 52 Nested if Statement 53 Identifying a Browser 55 switch case Statement 58 Loop Statement 62 The for Loop 62 The for in Loop 66 The while Loop 68 The do while Loop 70 continue 71 Looking Ahead 72 Quiz 73 CHAPTER 4 Arrays 75 What Is an Array? 75 Declaring an Array 76 Initializing an Array 77 Defi ning Array Elements 78 How Many Elements Are in the Array? 79 Looping the Array 80 Adding an Array Element 82 Sorting Array Elements 83 CONTENTS ix Making a New Array from an Existing Array 84 Combining Array Elements into a String 87 Changing Elements of the Array 90 Looking Ahead 91 Quiz 92 CHAPTER 5 Functions 95 What Is a Function? 96 Defi ning a Function 96 Writing a Function Defi nition 98 Adding Arguments 99 The Scope of Variables and Arguments 100 Calling a Function 101 Calling a Function Without an Argument 102 Calling a Function with an Argument 103 Calling a Function from HTML 104 Functions Calling Another Function 108 Returning Values from a Function 109 Looking Ahead 113 Quiz 114 CHAPTER 6 Strings 117 Why Manipulate a String? 117 Joining Strings 118 Finding Your Way Around a String 120 Dividing Text 123 Copying a Substring 125 Converting Numbers and Strings 129 Numbers to Strings 130 Changing the Case of the String 131 Strings and Unicode 132 Looking Ahead 132 Quiz 133 [...]... apprehensive learning JavaScript, especially if you are a web developer and not a computer programmer JavaScript can be mystifying; however, as you read JavaScript Demystified you’ll quickly untangle the mystery, because your knowledge of HTML is used as the foundation for learning to write JavaScripts As you’ll see when you write your first JavaScript in Chapter 1, each element of JavaScript is introduced... You can copy examples illustrated in this book from our web site (www .osborne. com) and experiment with each JavaScript concept presented in this book Load the web page and see the affect of the JavaScript Comment out the JavaScript and reload the web page and see how the page reacts without the JavaScript Once you’ve mastered the JavaScript technique, you can incorporate it into your own web page and... INTRODUCTION xvii Chapter 1: An Inside Look at JavaScript Chapter 1 sets the stage for the rest of the book by presenting the bare facts of JavaScript You’ll explore the basic concepts of JavaScript and learn what JavaScript can do and what it cannot do Most importantly, you’ll create your first working JavaScript It won’t wow anyone but yourself, but your first JavaScript breaks through the unknown and lets... list selection changes The JavaScript reads the selection and determines the settings for the other form elements You’ll learn how to perform this and other feats of JavaScript magic in this chapter Chapter 8: Cookies What does a baker, Cookie Monster, and JavaScript have in common? Cookies! A baker and JavaScript make cookies Cookie Monster and JavaScript eat cookies (That is, JavaScript kind of eats... get started writing your first JavaScript, hold on; you’ll do this a little later in the chapter Before jumping in over your head, let’s take a moment and explore the basic concepts of JavaScript 1 Copyright © 2005 by The McGraw-Hill Companies, Inc Click here for terms of use 2 JavaScript Demystified Answers to Common Questions About JavaScript Many developers who are new to JavaScript are puzzled by the... browser window when displaying a JavaScript form A JavaScript program cannot access your computer’s hard disk Is JavaScript the same as VBScript and JScript? The simple answer is no VBScript and JScript were developed by Microsoft to create interactive web pages JavaScript is a Netscape creation JavaScript: A Limited-Featured Programming Language You can do many things using JavaScript that you can’t do... most exciting to write, but the more exciting JavaScripts are yet to come For now, it is important that you learn how to write a basic JavaScript A JavaScript consists of JavaScript statements that are placed within the HTML tags in a web page This means that you don’t need any special tools to write a JavaScript You can use the same tools to write a JavaScript that you use to write your web page... Browsers that are JavaScript- enabled recognize and run a JavaScript that is contained within an HTML comment Older browsers simply ignore the JavaScript, thinking that the script is a comment The following listing illustrates how to hide your JavaScript from older browsers Notice that the HTML comment is placed inside the and tags and around the JavaScript code Some rookie JavaScript developers... Hiding Hello world! JavaScript Figure 1-2 The alert dialog box remains on the screen until the OK button or the close box is clicked JavaScript Demystified 12 Looking Ahead Now you have a pretty good understanding of what JavaScript is and what it isn’t JavaScript is a limited-featured... understanding of JavaScript and know how to write a simple JavaScript application, it is time to move on to more interesting aspects of JavaScript In the next chapter you’ll learn how to store and use information within a JavaScript Quiz 1 JavaScript is a version of a Java b LiveScript c C++ d VBScript CHAPTER 1 An Inside Look at JavaScript 2 A JavaScript must reside within the a tag b . TEAM LinG JAVASCRIPT DEMYSTIFIED This page intentionally left blank. JAVASCRIPT DEMYSTIFIED JIM KEOGH McGraw-Hill/ Osborne New York Chicago San Francisco Lisbon. xv CHAPTER 1 An Inside Look at JavaScript 1 Answers to Common Questions About JavaScript 2 JavaScript: A Limited-Featured Programming Language 3 Getting Down to JavaScript 4 Object Name 4 Property. foundation for learning to write JavaScripts. As you’ll see when you write your fi rst JavaScript in Chapter 1, each element of JavaScript is introduced by combining just the JavaScript element with a