100% found this document useful (1 vote)
20 views

Sams Teach Yourself JavaScript in 24 Hours 4th Edition Michael Moncur - Quickly access the ebook and start reading today

The document provides links to download the full version of 'Sams Teach Yourself JavaScript in 24 Hours 4th Edition' by Michael Moncur, along with several other recommended ebooks and textbooks available at ebookultra.com. It includes details about the book such as ISBN, file size, and publication year. Additionally, it outlines the contents and structure of the JavaScript book, covering various topics from basic concepts to advanced features.

Uploaded by

aaljanaxis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
20 views

Sams Teach Yourself JavaScript in 24 Hours 4th Edition Michael Moncur - Quickly access the ebook and start reading today

The document provides links to download the full version of 'Sams Teach Yourself JavaScript in 24 Hours 4th Edition' by Michael Moncur, along with several other recommended ebooks and textbooks available at ebookultra.com. It includes details about the book such as ISBN, file size, and publication year. Additionally, it outlines the contents and structure of the JavaScript book, covering various topics from basic concepts to advanced features.

Uploaded by

aaljanaxis
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Visit https://ebookultra.

com to download the full version and


explore more ebooks or textbooks

Sams Teach Yourself JavaScript in 24 Hours 4th


Edition Michael Moncur

_____ Click the link below to download _____


https://ebookultra.com/download/sams-teach-yourself-
javascript-in-24-hours-4th-edition-michael-moncur/

Explore and download more ebooks or textbooks at ebookultra.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Sams Teach Yourself Macromedia Flash MX 2004 in 24 Hours


Sams Teach Yourself Phillip Kerman

https://ebookultra.com/download/sams-teach-yourself-macromedia-flash-
mx-2004-in-24-hours-sams-teach-yourself-phillip-kerman/

Sams Teach Yourself to Create Web Pages in 24 Hours 4th


Edition Ned Snell

https://ebookultra.com/download/sams-teach-yourself-to-create-web-
pages-in-24-hours-4th-edition-ned-snell/

Swift in 24 Hours Sams Teach Yourself 2nd edition Edition


Miller

https://ebookultra.com/download/swift-in-24-hours-sams-teach-
yourself-2nd-edition-edition-miller/

Sams Teach Yourself TCP IP in 24 Hours 2nd Edition Joe


Casad

https://ebookultra.com/download/sams-teach-yourself-tcp-ip-
in-24-hours-2nd-edition-joe-casad/
Sams Teach Yourself ASP NET in 24 Hours 1st Edition Joe
Martin

https://ebookultra.com/download/sams-teach-yourself-asp-net-
in-24-hours-1st-edition-joe-martin/

Sams Teach Yourself Android Application Development in 24


Hours 2nd Edition Lauren Darcey

https://ebookultra.com/download/sams-teach-yourself-android-
application-development-in-24-hours-2nd-edition-lauren-darcey/

Sams teach yourself TCP IP in 24 hours 3rd ed Edition Joe


Casad

https://ebookultra.com/download/sams-teach-yourself-tcp-ip-
in-24-hours-3rd-ed-edition-joe-casad/

Sams Teach Yourself HTML and XHTML in 24 Hours 5th Edition


Dick Oliver

https://ebookultra.com/download/sams-teach-yourself-html-and-xhtml-
in-24-hours-5th-edition-dick-oliver/

Sams Teach Yourself Microsoft Expression Web 3 in 24 Hours


1st Edition Morten Rand-Hendriksen

https://ebookultra.com/download/sams-teach-yourself-microsoft-
expression-web-3-in-24-hours-1st-edition-morten-rand-hendriksen/
Sams Teach Yourself JavaScript in 24 Hours 4th Edition
Michael Moncur Digital Instant Download
Author(s): Michael Moncur
ISBN(s): 9780672328794, 0672328798
File Details: PDF, 4.77 MB
Year: 2010
Language: english
Teach
Yourself

JavaScript

Michael Moncur
in 24
Hours
800 East 96th Street, Indianapolis, Indiana, 46240 USA
Sams Teach Yourself JavaScript in 24 Hours Acquisitions Editor
Betsy Brown
Copyright  2007 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a Development Editor
retrieval system, or transmitted by any means, electronic, mechanical, Songlin Qiu
photocopying, recording, or otherwise, without written permission from
the publisher. No patent liability is assumed with respect to the use of Managing Editor
the information contained herein. Although every precaution has been Patrick Kanouse
taken in the preparation of this book, the publisher and author assume
no responsibility for errors or omissions. Nor is any liability assumed for Senior Project Editor
damages resulting from the use of the information contained herein. Matthew Purcell
International Standard Book Number: 0-672-32879-8
Copy Editor
Library of Congress Catalog Card Number: 2005909315 Jessica McCarty
Printed in the United States of America
Indexer
First Printing: July 2006
Tim Wright
09 08 07 06 4 3 2 1
Proofreader
Trademarks Carla Lewis
All terms mentioned in this book that are known to be trademarks or
Technical Editor
service marks have been appropriately capitalized. Sams Publishing
Jim O’Donnell
cannot attest to the accuracy of this information. Use of a term in this
book should not be regarded as affecting the validity of any trademark
Publishing Coordinator
or service mark.
Vanessa Evans

Warning and Disclaimer Book Designer


Every effort has been made to make this book as complete and as accu- Gary Adair
rate as possible, but no warranty or fitness is implied. The information
provided is on an “as is” basis. The author and the publisher shall have Page Layout
neither liability nor responsibility to any person or entity with respect to TnT Design, Inc.
any loss or damages arising from the information contained in this
book.

Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in
quantity for bulk purchases or special sales. For more information,
please contact
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
Introduction. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

Part I: Introducing the Concept of Web Scripting


and the JavaScript Language

HOUR 1 Understanding JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7


2 Creating Simple Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3 Getting Started with JavaScript Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
4 Working with the Document Object Model (DOM) . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Part II: Learning JavaScript Basics

HOUR 5 Using Variables, Strings, and Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63


6 Using Functions and Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
7 Controlling Flow with Conditions and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
8 Using Built-in Functions and Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

Part III: Learning More About the DOM

HOUR 9 Responding to Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139


10 Using Windows and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
11 Getting Data with Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

12 Working with Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191


13 Using the W3C DOM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

14 Using Advanced DOM Features. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

Part IV: Working with Advanced JavaScript Features

HOUR 15 Unobtrusive Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235


16 Debugging JavaScript Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
17 AJAX: Remote Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
18 Greasemonkey: Enhancing the Web with JavaScript . . . . . . . . . . . . . . . . . . . . . . . 293

Part V: Building Multimedia Applications with JavaScript

HOUR 19 Using Graphics and Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313

20 Working with Sound and Plug-ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329


Part VI: Creating Complex Scripts

HOUR 21 Building JavaScript Drop-down Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345


22 Creating a JavaScript Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
23 Creating JavaScript Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
24 Your Future with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393

Part VII: Appendixes

A Other JavaScript Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409


B Tools for JavaScript Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
C Glossary. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
D JavaScript Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
E DOM Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
Table of Contents
Part I: Introducing the Concept of Web Scripting and the JavaScript
Language

HOUR 1: Understanding JavaScript 7


Learning Web Scripting Basics ................................................................7

How JavaScript Fits into a Web Page ........................................................9

Browsers and JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12


Specifying JavaScript Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
JavaScript Beyond the Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

Exploring JavaScript’s Capabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16


Alternatives to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

HOUR 2: Creating Simple Scripts 23


Tools for Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Displaying Time with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

Beginning the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

Adding JavaScript Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26


Creating Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Adding the Script to a Web Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Testing the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

HOUR 3: Getting Started with JavaScript Programming 37


Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
JavaScript Syntax Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

Using Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

Best Practices for JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

HOUR 4: Working with the Document Object Model (DOM) 49


Understanding the Document Object Model (DOM) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

Using Window Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51


Working with Web Documents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

Accessing Browser History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55

Working with the Location Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55


vi
Sams Teach Yourself JavaScript in 24 Hours

Part II: Learning JavaScript Basics

HOUR 5: Using Variables, Strings, and Arrays 63


Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

Understanding Expressions and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

Data Types in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

Converting Between Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69

Using String Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70

Working with Substrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74

Using Numeric Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76

Using String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77


Sorting a Numeric Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

HOUR 6: Using Functions and Objects 85


Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

Introducing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
Using Objects to Simplify Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

Extending Built-in Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94

HOUR 7: Controlling Flow with Conditions and Loops 101


The if Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102

Using Shorthand Conditional Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105


Testing Multiple Conditions with If and Else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

Using Multiple Conditions with switch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107

Using for Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109


Using While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

Using Do…While Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

Working with Loops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112


Looping Through Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114

HOUR 8: Using Built-in Functions and Libraries 121


Using the Math Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
Working with Math Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
Using the with Keyword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
vii
Contents

Working with Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126


Using Third-Party Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
Other Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

Part III: Learning More About the DOM

HOUR 9: Responding to Events 139


Understanding Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139

Using Mouse Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144


Using Keyboard Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Using the onLoad and onUnload Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

HOUR 10: Using Windows and Frames 157


Controlling Windows with Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
Moving and Resizing Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160

Using Timeouts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162


Displaying Dialog Boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

Working with Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

HOUR 11: Getting Data with Forms 173


The Basics of HTML Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173

Using the Form Object with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 174

Scripting Form Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176


Displaying Data from a Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

Sending Form Results by Email . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

HOUR 12: Working with Style Sheets 191


Style and Substance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 191
Defining and Using CSS Styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Using CSS Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

Creating a Simple Style Sheet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 198

Using External Style Sheets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200


Controlling Styles with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
viii
Sams Teach Yourself JavaScript in 24 Hours

HOUR 13: Using the W.3C DOM 207


The DOM and Dynamic HTML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

Understanding DOM Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

Creating Positionable Elements (Layers) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210

HOUR 14: Using Advanced DOM Features 219


Working with DOM Nodes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219

Hiding and Showing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222


Modifying Text Within a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
Adding Text to a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225

Part IV: Working with Advanced JavaScript Features

HOUR 15: Unobtrusive Scripting 235


Scripting Best Practices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
Reading Browser Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

Cross-Browser Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245

Supporting Non-JavaScript Browsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247

HOUR 16: Debugging JavaScript Applications 255


Avoiding Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

Basic Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258


Creating Error Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260

Advanced Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

HOUR 17: AJAX: Remote Scripting 273


Introducing AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

Using XMLHttpRequest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

Creating a Simple AJAX Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

Creating an AJAX Quiz Using the Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280


Debugging AJAX Applications. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 285

HOUR 18: Greasemonkey: Enhancing the Web with JavaScript 293


Introducing Greasemonkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Working with User Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296
Creating Your Own User Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
ix
Contents

Part V: Building Multimedia Applications with JavaScript

HOUR 19: Using Graphics and Animation 313


Using Dynamic Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313

Creating Rollovers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 315


A Simple JavaScript Slideshow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

HOUR 20: Working with Sound and Plug-Ins 329


Introducing Plug-Ins . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

JavaScript and Flash . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332

Playing Sounds with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333

Testing Sounds in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

Part VI: Creating Complex Scripts

HOUR 21: Building JavaScript Drop-Down Menus 345


Designing Drop-Down Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

Scripting Drop-Down Menu Behavior . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 350

HOUR 22: Creating a JavaScript Game 359


About the Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Creating the HTML Document. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361
Creating the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

Adding Style with CSS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 368

HOUR 23: Creating JavaScript Applications 377


Creating a Scrolling Window. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377
Style Sheet Switching with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

HOUR 24: Your Future with JavaScript 393


Learning Advanced JavaScript Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393

Future Web Technologies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 394

Planning for the Future . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 397

Moving on to Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398


Part VII: Appendixes

APPENDIX A: Other JavaScript Resources 409


Other Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
JavaScript Websites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409

Web Development Sites . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

This Book’s Website . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 410

APPENDIX B: Tools for JavaScript Developers 411


HTML and Text Editors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
HTML Validators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Debugging Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

APPENDIX C: Glossary 415

APPENDIX D: JavaScript Quick Reference 419


Built-in Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Creating and Customizing Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
JavaScript Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

JavaScript Built-in Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

APPENDIX E: DOM Quick Reference 427


DOM Level 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 427

DOM Level 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433
About the Author
Michael Moncur is a freelance webmaster and author. He runs a network of websites,
including the Web’s oldest site about famous quotations, online since 1994. He wrote Sams
Teach Yourself DHTML in 24 Hours, and has also written several bestselling books about net-
working, certification programs, and databases. He lives with his wife in Salt Lake City,
Utah.

Dedication
To my family, and especially Laura. Thanks for all your love and support.

Acknowledgments
I’d like to thank everyone at Sams for their help with this book, and for the opportunity to
write it. In particular, Betsy Brown got this edition started and kept it moving. Songlin Qiu
managed the development of the book. Project editor Matt Purcell handled the editing
process, and the copy editor, Jessica McCarty, saved me from many embarrassing errors.
The technical reviewer, Jim O’Donnell, painstakingly tested the scripts and helped keep the
writing grounded in reality.

I am grateful to everyone involved with previous editions of this book, including Scott
Meyers, David Mayhew, Sean Medlock, Susan Hobbs, Michelle Wyner, Jeff Schultz, Amy
Patton, George Nedeff, and Phil Karras. I’d also like to thank Neil Salkind and the rest of
the team at Studio B for their help throughout this project.

Finally, personal thanks go to my wife, Laura; my parents, Gary and Susan Moncur; the
rest of the family; and my friends, particularly Chuck Perkins, Matt Strebe, Cory Storm,
Robert Parsons, Dylan Winslow, Ray Jones, Tyson Jensen, Curt Siffert, Richard Easlick, and
Henry J. Tillman. I couldn’t have done it without your support.
xii
Sams Teach Yourself JavaScript in 24 Hours

We Want to Hear from You!


As the reader of this book, you are our most important critic and commentator. We value
your opinion and want to know what we’re doing right, what we could do better, what
areas you’d like to see us publish in, and any other words of wisdom you’re willing to pass
our way.

You can email or write me directly to let me know what you did or didn’t like about this
book—as well as what we can do to make our books stronger.

Please note that I cannot help you with technical problems related to the topic of this book, and
that due to the high volume of mail I receive, I might not be able to reply to every message.

When you write, please be sure to include this book’s title and author as well as your name
and phone or email address. I will carefully review your comments and share them with the
author and editors who worked on the book.

Email: [email protected]

Mail: Mark Taber


Associate Publisher
Sams Publishing
800 East 96th Street
Indianapolis, IN 46240 USA

Reader Services
Visit our website and register this book at www.samspublishing.com/register for convenient
access to any updates, downloads, or errata that might be available for this book.
Introduction

The World Wide Web began as a simple repository for information, but it has grown into
much more—it entertains, teaches, advertises, and communicates. As the Web has evolved,
the tools have also evolved. Simple markup tools such as HTML have been joined by true
programming languages—including JavaScript.

Now don’t let the word “programming” scare you. For many, the term conjures up images of
long nights staring at the screen, trying to remember which sequence of punctuation marks
will produce the effect you need. (Don’t get me wrong—some of us enjoy that sort of thing.)

Although JavaScript is programming, it’s a very simple language. As a matter of fact, if you
haven’t programmed before, it makes a great introduction to programming. It requires very
little knowledge to start programming with JavaScript—you’ll write your first program in
Hour 2, “Creating Simple Scripts.”

If you can create a web page with HTML, you can easily use JavaScript to improve a page.
JavaScript programs can range from a single line to a full-scale application. In this book,
you’ll start with simple scripts, and proceed to complex applications, such as a card game.
You’ll also explore some of the most recent uses of JavaScript, such as AJAX remote scripting.

If you’ve spent much time developing pages for the Web, you know that the Web is con-
stantly changing, and it can be hard to keep up with the latest languages and tools. This
book will help you add JavaScript to your web development toolbox, and I think you’ll
enjoy learning it.

JavaScript and Web Standards


When JavaScript first appeared in browsers, it had rather limited capabilities, and
JavaScript programmers have always pushed the envelope to take maximum advantage of
what the language was capable of. Unfortunately, this resulted in some bad practices, such
as scripts that only worked in one browser, and JavaScript gained a bit of a bad reputation.

Now, thanks to wide browser support for standards established by the W3C (World Wide
Web Consortium) and new technologies such as AJAX, JavaScript’s future is looking brighter
than ever, and a new, more responsible style of scripting is gaining favor. Unobtrusive
scripting focuses on adding interactive features while keeping the HTML simple and
standards-compliant.
2
Sams Teach Yourself JavaScript in 24 Hours

Throughout this book, you’ll learn the best practices for using JavaScript responsibly and fol-
lowing web standards. All of the examples in this book avoid browser-specific techniques in
favor of standard techniques, and all of the examples will work in most modern browsers.

How to Use This Book


This book is divided into 24 lessons. Each covers a single JavaScript topic, and should take
about an hour to complete. The lessons start with the basics of JavaScript, and continue
with more advanced topics. You can study an hour a day, or whatever pace suits you. (If
you choose to forego sleep and do your studying in a single 24-hour period, you might have
what it takes to be a computer book author.)

Organization of This Book


This book is divided into six parts, each focusing on one area of JavaScript:

. Part I, “Introducing the Concept of Web Scripting and the JavaScript Language,”
introduces JavaScript, describes how it fits in with other languages, and explains the
basic language features of JavaScript. It also introduces the DOM (Document Object
Model), which connects JavaScript to web documents.

. Part II, “Learning JavaScript Basics,” covers the fundamentals of the JavaScript lan-
guage: variables, functions, objects, loops and conditions, and built-in functions.
You’ll also learn about third-party libraries that add functionality to JavaScript.

. Part III, “Learning More About the DOM,” digs deeper into the DOM objects you’ll use
in nearly every JavaScript program. It covers events, windows, and web forms. You’ll
also learn about CSS style sheets, and the DOM features that enable you to change
styles. Finally, you’ll learn about the W3C DOM, which enables you to modify any
part of a page using JavaScript.

. Part IV, “Working with Advanced JavaScript Features,” begins with a look at unobtrusive
scripting techniques to keep JavaScript from intruding on the functionality and validity
of HTML documents. You’ll also learn how to debug JavaScript applications, and finally
take a look at two cutting-edge JavaScript features: AJAX and Greasemonkey.

. Part V, “Building Multimedia Applications with JavaScript,” describes JavaScript’s fea-


tures for working with graphics, animation, sound, and browser plug-ins.

. Part VI, “Creating Complex Scripts,” focuses on helping you create complete
JavaScript applications. You’ll learn how to create drop-down menus, a card game
written in JavaScript, and other examples. In the last hour, you’ll learn about what’s
in store for JavaScript and what other languages you might want to learn next.
3
Introduction

Conventions Used in This Book


This book contains special elements as described by the following:

These boxes highlight information that can make your JavaScript Did you
programming more efficient and effective. Know?

These boxes provide additional information related to material you just By the
read. Way

These boxes focus your attention on problems or side effects that can Watch
occur in specific situations. Out!

A special monospace font is used on programming-related terms and language.

Try It Yourself ▼
The Try It Yourself section at the end of each chapter guides you through the process of
creating your own script or applying the techniques learned throughout the hour. This
will help you create practical applications of JavaScript based on what you’ve learned. ▲

Q&A, Quiz, and Exercises


At the end of each hour’s lesson, you’ll find three final sections. Q&A answers a few of the
most common questions about the hour’s topic. The Quiz tests your knowledge of the skills
you learned in that hour, and the Exercises offer ways for you to gain more experience with
the techniques the hour covers.

This Book’s Website


Because JavaScript and the Web are constantly changing, you’ll need to stay up-to-date
after reading this book. This book’s website includes the latest updates as well as download-
able versions of the listings and graphics for the examples used in this book. To access the
book’s website, register your book at http://www.samspublishing.com/register.
4
Sams Teach Yourself JavaScript in 24 Hours

The Author’s Website


The author of this book, Michael Moncur, maintains a website about JavaScript at
http://www.jsworkshop.com/. There you’ll find regular updates on the JavaScript language
and the DOM, links to script examples, and detailed tutorial articles.

If you have questions or comments about this book, have noticed an error, or have
trouble getting one of the scripts to work, you can also reach the author by email at
[email protected]. (Please check the website first to see if your question has been
answered.)
PART I:
Introducing the Concept of
Web scripting and the
JavaScript Language

HOUR 1 Understanding JavaScript 7

HOUR 2 Creating Simple Scripts 23

HOUR 3 Getting Started with JavaScript Programming 37

HOUR 4 Working with the Document Object Model (DOM) 49


This page intentionally left blank
HOUR 1
Understanding JavaScript

What You’ll Learn in This Hour:


. What web scripting is and what it’s good for
. How scripting and programming are different (and similar)
. What JavaScript is and where it came from
. How to include JavaScript commands in a web page
. How different browsers handle JavaScript
. What JavaScript can do for your web pages
. How to choose between JavaScript and alternative languages

The World Wide Web (WWW) began as a text-only medium—the first browsers didn’t even
support images within web pages. Although it’s still not quite ready to give television a
run for its money, the Web has come a long way since then.

Today’s websites can include a wealth of features: graphics, sounds, animation, video, and
occasionally useful content. Web scripting languages, such as JavaScript, are one of the
easiest ways to spice up a web page and to interact with users in new ways.

The first hour of this book introduces the concept of web scripting and the JavaScript lan-
guage. It also describes how JavaScript fits in with other web languages.

Learning Web Scripting Basics


In the world of science fiction movies (and many other movies that have no excuse), com-
puters are often seen obeying commands in English. Although this might indeed happen
in the near future, computers currently find it easier to understand languages such as
BASIC, C, and Java.

If you know how to use HTML (Hypertext Markup Language) to create a web document,
you’ve already worked with one computer language. You use HTML tags to describe how
Other documents randomly have
different content
O Seigneur Dieu!
Her death! I do not wish her death! Not I!
O Our Lady! let not ill thoughts possess me!
I would I were at Morbec this still eve,
Herding the cows amid the golden broom,
Above a sea of glass without a wind,
As stagnant calm as is this prisoned water!
I would gather the musk rose in the lane,
I would tread the wet sand and count the ships,
My brow would not burn, my heart would not ache,
No tears from my eyes would I wipe away!
Why should they not fall like the winter rain?
I am the herd girl here as at Morbec,
And she’s a great lady, loved for herself!
O love! is it love that stifles me so?
O love! is it love that makes me weep?
I thought that love was all splendour and light,
The bow in the sky, the bird at its height,
The glory and state of an angel bright!
What is this pain that burdens all my heart?

[She bows her head upon her knees. The hum of the
street deepens to a continuous and sinister sound.
In the distance a roll of drums. Yvette raises her
head.

I sit by this fountain, he’ll not return!


He cares not for me,—he’s the Sieur de Morbec,
And I a herd girl wandering through his fields!
Mother, my mother, did you sit and wait,
By the wild sea rim on a glowing eve,
Mid the brown seaweed on the shining sands?
Your heart did it beat, and your senses swim?—
But your lover, the fisher, he came, he came!
[The voice of the street deepens.

I will not have this pain! I’ll tear it out!

[Her hand touches the purple mark on her throat.

Ha! how burns this hateful mark to-day!

[There comes from the church towers of Nantes a


sudden and violent crash of bells.

Sister Fidelis (rising)

The tocsin!

The Young Girls (They flutter forward to the


fountain)

The tocsin! Oh, the tocsin!


Like a hive of bees hums the street without!

Yvette

Oh, all ye iron bells! ring on! ring on!

Enter Mlle. de Château-Gui and Sister Benedicta.

The Young Girls

Here is Mademoiselle de Château-Gui!


She’ll tell us why the bells are ringing!

Mlle. de Château-Gui
O Ciel!
Would you believe it? O blessed saints above!
The country is in danger!

A Young Girl

Oh! we thought
You brought us news!

Mlle. de Château-Gui (joyously)

Do you not hear the bells?


Oh, such a day outside! It is proclaimed!
La patrie est en danger!

[Distant trumpets.

Well you may wail,


You brazen trumpets of the Revolution!
The Duke of Brunswick he is marching now,
And with him all our nobles back from Coblentz!
O bliss! La patrie est en danger!

Sister Fidelis

Oh, hush!
The very walls have ears!

Mlle. de Château-Gui
My father says
The King shall have his own again, and all
Will go as merry as a wedding bell!
La patrie est en danger!

Enter Count Louis, Melipars de L’Orient, and the


Abbé de Barbasan.

Oh, here are


My father and Monsieur de L’Orient!

De L’Orient

So sweet the flowers here—

Count Louis (to the young girls)

Mesdemoiselles,
One garden of rosebuds time hath not touched!
(To the Sisters.) In your prayers, my Sisters, name Château-Gui!

[The young girls curtesy, then exeunt between the


trees. Yvette remains beside the fountain. Count
Louis looks at her through his glass.

Ha!

De L’Orient

The herd girl of Morbec!

Count Louis
I have eyes,
De L’Orient!

The Abbé

Hm!—Fair child!

Yvette (coldly)

Citoyen!

Mlle. de Château-Gui

Monsieur de L’Orient, you promised me


My father should not walk abroad to-day!

De L’Orient

What could I do? He is so young and rash!

Count Louis (taking snuff)

‘Tis true that Nantes is dangerous to-day


To all save those wild beasts the sans-culottes!
But that’s no reason I should stay at home.
Where is De Vardes? His man said he was here.
It is his wont, pardieu!

Sister Fidelis
Monsieur le Comte,
Monsieur the Baron of Morbec did come
To see that all was well with this our charge—
A peasant girl, monsieur, whom he did save
From cold and hunger and ill company.
But now she prospers and we think that he
Will come no more.

Yvette

Jesu Maria!

Count Louis (with satisfaction)

Ma foi!
He is a soldier is De Vardes! He camps
One day beside the hedgerow in the field!
The next he’s for some royal mount of love,
High as the snow and splendid in the sun!
Since he’s not here I know where else he is!

De L’Orient (sings)

Mignonne, Mignonne!
Kiss me, rose of to-day!

Yvette

O heart! O world! O hedgerow in the field!

Count Louis
Well, well, her mother was as fair as she!
Clarice de Miramand, long-dead Clarice!
Her hair was golden too.—Old times, old times!
And now it is De Vardes and the Marquise!

[Count Louis, Mlle. de Château-Gui, and De


L’Orient walk up and down beneath the trees. De
L’Orient sings.

De L’Orient

Mignonne, Mignonne!
The red rose fades away!
Mignonne, Mignonne!
The white rose will not stay!

The Abbé

My dear, that is a pretty wrist of thine!

Yvette

Citoyen!

The Abbé

Hast said thy rosary to-day?

Yvette

Citoyen!

The Abbé
A melting eye!

Yvette

Citoyen!

The Abbé

Dame! She is only good to burn châteaux!

[He joins Count Louis, etc. They walk and talk


beneath the trees.

Yvette

The high of heart bide no man’s scorning! I


Will break these bonds! I will be free! I will!
O royal mount of love, snow-high, sun-kissed,
Kissed by the sun which once did shine on me!
If I am of the fields—

[Her hand touches the mark upon her throat. She


laughs.
O hated flower,
Which grew beneath no hedgerow on this earth!
Teach me, thou poison blossom, pride of heart!
Where is that Duchess Jeanne whom I am like?
They say for love her heart did rive in twain,
But now she smiles beside a shadowy stream
In some far land where none do die of love!
And where is he, Jehan the fisherman,
Who loved Yvonne, who met the sea and died?
They died for love who should have lived for hate!
I’ll live—

Enter De Vardes. Count Louis, etc., come forward.

Oh, here’s the soldier! Now we’ll know


How blow the winds around the camp of love!

Count Louis

What is it, René de Vardes? What is it, man?

De Vardes

The King hath left the Tuileries! The mob


Forced the château and put his life in danger.
The Swiss are murdered, cut down to a man!
The Grenadiers joined with the Marseillaise!
De Maillé writes—the courier’s just arrived—
All is distraction, danger, and despair!

Sister Fidelis

Alas!
Mlle. de Château-Gui

O Ciel!

The Abbé

The soldiers in revolt.

De L’Orient

The Swiss all murdered—the stanch Swiss!

Sister Simplicia

Alas!

Count Louis

The King hath left the Tuileries!

De Vardes

To-night
I ride to Paris.

Yvette

O God!

The Abbé
To Paris!
As well say that you ride to death, De Vardes!

Count Louis

Ah, were I young again, I’d ride with you!

Sister Fidelis

Alas, they say it is a fearful place!

Sister Simplicia

It is so safe in Nantes!

De Vardes

Ah, my Sister,
Because it is so safe in Nantes I go!
Once I did love this people; once I thought
Beyond this Revolution lay the morn,
The dewy morn of a most noble day!
It may be so; I know not; but I am
A soldier of the King. Needs must I go,
My bugles call; I’m breaking camp. Farewell!

Sister Fidelis

You will return.

De Vardes
If I’m in life I will!

Yvette

O Our Lady! O Our Lady!

[The noise in the street increases. The tocsin rings.


The sky begins to darken before an approaching
storm.

Count Louis

Ring on!
Ye bells! ring on to the deaf sky! O France,
Of old thou wast a pleasant land and free,
In palace and in field a courteous place!
Now thou art desolate! Come, Austria, come!
Come, D’Artois, come, Brunswick, and come, Provence!
Rend the tricolour from the breast of France
And plant the fleur-de-lis where stood the Jacobins!

Voices (from the street)

Quoi! ces cohortes étrangères


Feraient la loi dans nos foyers!

Mlle. de Château-Gui

Hast said farewell to the Marquise?

De Vardes
Not yet,
As far as Vannes I ride beside her coach.

Yvette

Oh!—

Mlle. de Château-Gui

Soon or late, she’ll draw you back to Nantes!


Now will she not?

De Vardes (smiling)

Perhaps.

Yvette

Jesu Maria!

Sister Fidelis

Monsieur, if you must go, oh, rest you sure


Jealously will we guard and spotless keep
The soul you stooped and drew from the foul mire!—
Yvette, come make your reverence to your lord!

Yvette

I kiss your hand, monseigneur!

The Abbé
There will be
A storm to-night!

Count Louis

Come, come, René de Vardes!


I’d see the courier who brought this news!

De Vardes

I’ll follow you, Monsieur le Comte!

[Exeunt Count Louis, his daughter, De L’Orient,


The Abbé, and the Sisters.

Yvette

Wilt thou go?

De Vardes

I must.

Yvette

Why must thou go?


To-day the kingdom fell! Oh, in the dust
Of old things let it rest for evermore!
Take up the Revolution!

[Lightning.
Oh, see!
The flaming sword before the gates of Eden!
Thou’rt safe within the garden! Go not forth.
Go not to Paris! Stay in Nantes, ah, stay!
Wear the tricolour—

[Thunder.

Hark! It is the voice,


The menacing voice of the Republic!
It threatens thee, it threatens all who pass
That flaming sword, to lift the thing that was
And is not any more! Oh, let it lie!—
Thou’lt not to Paris?

De Vardes

To-night, Citoyenne!
Ah, thou art skilful at betraying!

Yvette

Quoi!

Enter Sister Benedicta.

Sister Benedicta

Monsieur le Baron de Morbec, the page


Of Madame la Marquise de Blanchefôret
Attends—

Yvette
Name of a name!

The Abbé (appearing in the door behind Sister


Benedicta)

De Vardes, De Vardes!
You gather the furze while the red rose waits!

De Vardes

At once, my Sister!

(To Yvette.) Ah, not in anger,


Must thou and I part for this little while!
If I’m in life I will return, be sure,
To Nantes and all this garden loveliness,
Those fruit trees and this fountain!—Fare thee well.
The nuns will care for thee; I’ve ordered all.
Too fierce of aspect is the world without!
Here is fair peace, security, and calm;
Here thou art fenced from storm and violence.
Abide thou here until I come again!

[Lightning.

Yvette

The flaming sword!

De Vardes

Hearest thou not, Yvette,


How sings the lark in Paimpont Wood to-day?
Yvette

I hear the dirge of the salt sea!

De Vardes

And there,
Seest thou not through yonder trees the stone,
The Druid Stone where thou didst lie in sleep?

Yvette

I see a broken fan!

De Vardes

Abide thou here


And dream of Paimpont Wood until I come.
I too will dream, I too will dream, Yvette!

Yvette

Is not Clarice a lovely name?

De Vardes

Why, yes,
A very lovely name.—Farewell, farewell!
I’ll see thy face, be sure, this very night,
Upon the road before me as I ride.

Yvette
Oh, fare you well beneath the silver moon
As slow you ride beside a lady’s coach,
Discoursing of the dazzling, snowy heights!
I kiss your hand, monseigneur! Fare you well!

[The Abbé’s voice is heard from the doorway.

The Abbé

De Vardes! De Vardes!

De Vardes

I come!

The Abbé

The rose awaits!—

Yvette

It is too much!

De Vardes

Farewell, thou spirit of Paimpont!

[Distant music.

Yvette

Ah, ah! ‘tis worth all else—the Marseillaise!


De Vardes

My Duchess Jeanne—

Yvette

She is dead: cold and dead!

Aux armes, Citoyens!


Formez vos bataillons!

De Vardes

Perverse and strange!

Yvette

I’ll to my beads. Adieu!

Over Ys, the sunken town,


When thou sailest look not down,
Mariner, mariner!

De Vardes

What wine hast thou drunken?

Yvette

An old wine—
For there dwells a fairy there
Will drag thee down by the long hair,
Mariner, mariner!

De Vardes

Oh, thou art too wilful!

The Abbé

De Vardes! De Vardes!

Yvette (to the fish in the fountain)

Gold fish, gold fish, how are the fish of Quiberon?

De Vardes

Thou sullen witch, adieu!

[Exit De Vardes.

Yvette

Monseigneur! ah!
He’s gone! He’s gone to meet the fairy queen!
He’s for the roses and the dazzling peaks!
The seaweed and the furze he’s left behind!
He’s left the storm, he’s left the storm and me!

[The convent bell rings.


Toll, toll! as though thou’d toll my soul away!
Thou canst not toll him back! Oh, woe is me!

[The nuns sing in the chapel.

Voices

O salutaris Hostia!
Quae coeli pandis ostium:
Bella premunt hostilia,
Da robur fer auxilium!

[Above the wall where it is shadowed by a fruit


tree, appear the head and shoulders of Lalain. He
draws himself up to the coping, watches Yvette
for a moment, then swings himself down to the garden.
He has a rose in his hand.

Yvette

Where is the sunshine gone? Where is the gold?


It was a lovely day! ‘Tis cold and dead;
No light, no warmth, no cheer!—Oh, presently
Those two will take the summer road to Vannes!
Ha! does he think that I will meekly stay
Within this convent close, will kneel and pray,
Day in, day out, for all true lovers’ weal?
What is there now to do?—O Jealousy!
I dream of Paimpont Wood in June! I’ll dream
Of sunlit peaks, of roses named Clarice;
I’ll dream of furze that’s set about with thorns
And clings unto the common earth which bore it!

[A roll of thunder.
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookultra.com

You might also like