
The Book of JavaScript: A Practical Guide to Interactive Web Pages by thau! ISBN: 1886411360 No Starch Press © 2000 (397 pages) A tutorial on JavaScript procedures. Table of Contents The Book Of Javascript— A Practical Guide to Interactive Web Pages Foreword Preface Chapter 1 - Welcome to JavaScript! Using Variables and Built-in Functions to Update Your Web Pages Chapter 2 - Automatically Chapter 3 - Give the Browsers What They Want Chapter 4 - Rollovers: Everyone’s Favorite JavaScript Trick Chapter 5 - Opening and Manipulating Windows Chapter 6 - Getting Functional: Writing Your Own JavaScript Functions Chapter 7 - Giving and Taking Information with Forms Chapter 8 - Keeping Track of Information with Arrays and Loops Chapter 9 - Timing Events Chapter 10 - Frames and Image Maps Chapter 11 - Validating Forms, Massaging Strings, and Working with CGI Chapter 12 - Cookies Chapter 13 - Dynamic HTML Chapter 14 - How to Fix Broken Code Appendix A - Beyond the Browser: Plug-ins, ActiveX, Making Music, and Java Appendix B - Reference to JavaScript Objects and Functions Appendix C - Answers to Assignments Index List of Figures List of Tables The Book Of Javascript— A Practical Guide to Interactive Web Pages thau! No Starch Press San Francisco All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. Printed in the United States of America 3 4 5 6 7 8 9 10—03 02 01 Trademarked names are used throughout this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Publisher: William Pollock Project Editor: Karol Jurado Assistant Editor: Nick Hoff Editorial Production Assistant: Jennifer Arter Cover and Interior Design: Octopod Studios Composition: Magnolia Studio Technical Editor: Nadav Savio Copyeditor: Gail Nelson Proofreader: Christine Sabooni Indexer: Nancy Humphreys Distributed to the book trade in the United States and Canada by Publishers Group West, 1700 Fourth Street, Berkeley, California 94710, phone: 800-788-3123 or 510-528- 1444, fax: 510-528-3444 For information on translations or book distributors outside the United States, please contact No Starch Press directly: No Starch Press 555 De Haro Street, Suite 250 San Francisco, CA 94107 phone: 415-863-9900; fax: 415-863-9950; [email protected]; www.nostarch.com The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it. Library of Congress Cataloging-in-Publication Data Thau, Dave. The book of JavaScript / Dave Thau. p. cm. Includes index. ISBN 1-886411-36-0 1. JavaScript (Computer program language) I. Title. QA76.73.J39 T37 2000 005.2'762--dc2 98-087609 Dedication I dedicate this, my first book, to the first people in my life. Thanks, mom and dad, for tricking me into becoming an engineer. And for everything else as well. Acknowledgments This book was truly a collaborative effort. Thanks to Nick Hoff for patiently editing my addled prose and giving me the various swift kicks I needed to finish this thing, William Pollock for talking me into writing this book and helping shape its overall direction, Nadav Savio for making sure my scripts actually worked, Karol Jurado and Gail Nelson for dotting my i’s and crossing my t’s, and Amanda Staab for getting the word out. And a big thanks to my sweetheart Kirsten, for tolerating the many missed weekends and the evening hours filled with the clamoring of this keyboard. Praise for The Book of Javascript “Real-world scripts that you can plug into your pages . [and] a patient tutor’s explanation of how they work.” —Jim Heid, Conference Chair, Thunder Lizard’s “Web Design” conferences “Thau’s JavaScript tutorials at Webmonkey.com are so insanely popular, they’ve earned him a Plato-like following of devoted fans. .Thanks to this book/CD-ROM combo . Thau-mania is destined to grow to Elvis proportions.” —Evany Thomas, Managing Editor of Webmonkey.com “…an approachable introduction to contemporary JavaScript scripting practices. Its clear and concise examples make an intimidating subject seem quite friendly.” —Michael Masumoto, Multimedia Studies Program Instructor at San Francisco State University Foreword I learned JavaScript completely on my own. There was no one to tell me about “event handlers” or how to set cookies. No one even explained what a variable is, let alone the best ways to name them. Of course I had reference books, but they were intimidating tomes, full of cryptic lists and tables, written by programmers for programmers. David Thau is a remarkable combination of natural teacher and seasoned programmer. As a result, The Book of JavaScript not only teaches JavaScript thoroughly and enjoyably in a friendly, unintimidating tone, but it teaches programming as elegantly as any book I’ve seen. In fact, I’ve always thought of this as Thau’s ulterior motive—he pretends he’s just showing you how to make a rollover or validate the text in an HTML form, but before you know it you’ve learned how to code! Perhaps the most telling thing I can say is that, reading this book, I can’t help but wish I was learning JavaScript for the first time. If you are, then consider yourself lucky to have Thau as a teacher. You couldn’t do better. Happy JavaScripting! Nadav Savio Preface You are about to begin a journey through JavaScript—a programming language that adds interactivity and spark to Web pages all over the Internet. This book, written primarily for nonprogrammers, provides scripts you can cut and paste into your Web site, but it also makes sure you understand how they work so you can write your own scripts. Each chapter in the book focuses on a few important JavaScript features, shows you how professional Web sites incorporate them, and takes you through examples of how you might add those features to your own Web pages. Before you dive in, here is a quick overview of what you’ll learn as you make your way through The Book of JavaScript. Chapter 1 lays out the book’s goals, describes JavaScript and compares it with other tools, explains some of the nifty ways in which JavaScript can enhance your Web pages, and takes you through writing your first JavaScript. Chapter 2 describes how JavaScript figures out what day it is and writes that date to a Web page. Along the way you’ll learn how JavaScript remembers things using variables and performs actions using functions. Chapter 3 shows you how to direct someone to a Web page specifically designed for his or her browser. This involves figuring out what browser the visitor is using, then making a decision based on that information using if-then statements and their kin. Chapter 4 covers that favorite JavaScript trick—an image swap. You’ll also learn how to trigger JavaScript based on a viewer’s actions. Chapter 5 tells you everything you need to know about opening new browser windows— the second-favorite JavaScript trick. You’ll also learn how JavaScript writes HTML to the new windows, closes them, and moves them around on screen. Chapter 6 explains the magic of writing your own JavaScript functions. Functions are the major building blocks of any JavaScript program, so learning to write your own is a critical step to JavaScript mastery. Chapter 7 shows you how JavaScript works with HTML forms to collect all kinds of information from your users and give them fancy ways to navigate your site. Chapter 8 describes how JavaScript deals with lists, whether they’re of all the images on a Web page or of all the friends in your address book. JavaScript calls lists arrays, and they come in very handy. Chapter 9 discusses setting events to occur at specific times. For example, you can open a window and then close it in 5 seconds, or you can write a clock that updates every second. Once you know how to do this, you can create games and other interactive applications based on timed events. Chapter 10 discusses how JavaScript works with HTML frames. It covers topics including changing two or more frames at once and preventing your Web pages from getting trapped in someone else’s frame set. Chapter 11 shows how to make sure people are filling out your HTML forms correctly. Along the way, you’ll learn fancy ways to check user input—for example, you can check the formatting of an email address. Chapter 12 covers cookies. These bits of code let your Web pages save information a visitor has provided even after he or she turns off the computer. This allows your site to greet guests by name whenever they visit (if they tell you their name, of course!). Chapter 13 introduces Dynamic HTML, a feature of newer browsers that lets you animate entire Web pages. Chapter 14 wraps things up by giving you tips on what to do when the JavaScript you’ve written isn’t working correctly. Appendix A covers how JavaScript can interact with other programs your browser may include—Java, ActiveX, and QuickTime, for example.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages333 Page
-
File Size-