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

Practical JavaScript DOM Scripting and Ajax Projects 1st Edition Frank Zammetti download

The document provides a list of various JavaScript and Ajax-related books available for download, including titles like 'Practical JavaScript DOM Scripting and Ajax Projects' by Frank Zammetti and 'JavaScript Ajax for Dummies' by Andy Harris. It includes links to download these books and highlights their contents, such as practical projects and techniques for web development. The document also contains publication details and acknowledgments for the authors and contributors involved in these works.

Uploaded by

zwsojhzvk529
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
9 views

Practical JavaScript DOM Scripting and Ajax Projects 1st Edition Frank Zammetti download

The document provides a list of various JavaScript and Ajax-related books available for download, including titles like 'Practical JavaScript DOM Scripting and Ajax Projects' by Frank Zammetti and 'JavaScript Ajax for Dummies' by Andy Harris. It includes links to download these books and highlights their contents, such as practical projects and techniques for web development. The document also contains publication details and acknowledgments for the authors and contributors involved in these works.

Uploaded by

zwsojhzvk529
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

Practical JavaScript DOM Scripting and Ajax Projects

1st Edition Frank Zammetti download pdf

https://ebookultra.com/download/practical-javascript-dom-scripting-and-
ajax-projects-1st-edition-frank-zammetti/

Visit ebookultra.com today to download the complete set of


ebook or textbook!
We believe these products will be a great fit for you. Click
the link to download now, or visit ebookultra.com
to discover even more!

DOM Scripting Web Design with JavaScript and the Document


Object Model 2nd Edition Jeremy Keith

https://ebookultra.com/download/dom-scripting-web-design-with-
javascript-and-the-document-object-model-2nd-edition-jeremy-keith/

DOM Scripting Web Design with JavaScript and the Document


Object Model Second Edition Jeremy Keith

https://ebookultra.com/download/dom-scripting-web-design-with-
javascript-and-the-document-object-model-second-edition-jeremy-keith/

Learn JavaScript and Ajax with w3Schools 1st Edition


W3Schools

https://ebookultra.com/download/learn-javascript-and-ajax-
with-w3schools-1st-edition-w3schools/

Zk Ajax without Javascript Framework 1st Edition Henri


Chen

https://ebookultra.com/download/zk-ajax-without-javascript-
framework-1st-edition-henri-chen/
JavaScript Ajax for Dummies 1. Edition Andy Harris

https://ebookultra.com/download/javascript-ajax-for-dummies-1-edition-
andy-harris/

Scripting in Java Integrating with Groovy and JavaScript


1st Edition Kishori Sharan

https://ebookultra.com/download/scripting-in-java-integrating-with-
groovy-and-javascript-1st-edition-kishori-sharan/

DHTML Utopia Modern Web Design Using JavaScript DOM 1st


edition Edition Stuart Langridge

https://ebookultra.com/download/dhtml-utopia-modern-web-design-using-
javascript-dom-1st-edition-edition-stuart-langridge/

AJAX Creating Web Pages with Asynchronous JavaScript and


XML 1st Edition Edmond Woychowsky

https://ebookultra.com/download/ajax-creating-web-pages-with-
asynchronous-javascript-and-xml-1st-edition-edmond-woychowsky/

MooTools Essentials The Official MooTools Reference for


JavaScript and Ajax Development 1st Edition Aaron Newton

https://ebookultra.com/download/mootools-essentials-the-official-
mootools-reference-for-javascript-and-ajax-development-1st-edition-
aaron-newton/
Practical JavaScript DOM Scripting and Ajax Projects
1st Edition Frank Zammetti Digital Instant Download
Author(s): Frank Zammetti
ISBN(s): 9781590598160, 1590598164
Edition: 1
File Details: PDF, 6.20 MB
Year: 2007
Language: english
Practical JavaScript™,
DOM Scripting, and
Ajax Projects

■■■

Frank W. Zammetti
Practical JavaScript™, DOM Scripting, and Ajax Projects
Copyright © 2007 by Frank W. Zammetti
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.
ISBN-13 (pbk): 978-1-59059-816-0
ISBN-10 (pbk): 1-59059-816-4
Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1
Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence
of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark
owner, with no intention of infringement of the trademark.
Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc., in the
United States and other countries. Apress, Inc., is not affiliated with Sun Microsystems, Inc., and this book
was written without endorsement from Sun Microsystems, Inc.
Lead Editor: Matthew Moodie
Technical Reviewer: Herman van Rosmalen
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Jason Gilmore, Jonathan Gennick,
Jonathan Hassell, James Huddleston, Chris Mills, Matthew Moodie, Jeff Pepper, Paul Sarknas, Dominic
Shakeshaft, Jim Sumser, Matt Wade
Project Manager: Tracy Brown Collins
Copy Edit Manager: Nicole Flores
Copy Editor: Marilyn Smith
Assistant Production Director: Kari Brooks-Copony
Production Editor: Laura Esterman
Compositor: Susan Glinert
Proofreaders: Lori Bring and April Eddy
Indexer: Broccoli Information Management
Cover Designer: Kurt Krames
Manufacturing Director: Tom Debolski
Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail [email protected], or
visit http://www.springeronline.com.
For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA
94710. Phone 510-549-5930, fax 510-549-5939, e-mail [email protected], or visit http://www.apress.com.
The information in this book is distributed on an “as is” basis, without warranty. Although every precaution
has been taken in the preparation of this work, neither the author(s) nor Apress 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 this work.
The source code for this book is available to readers at http://www.apress.com in the Source Code/Download
section.
Dedicated to all the animals I’ve eaten over the years, without whom I
most certainly would have died a long time ago due to starvation. Well, I suppose
I could have been a vegan, but then I’d have to dedicate this to all the plants
I’ve eaten, and that would just be silly because very few plants can read.

To all my childhood friends who provided me with cool stories to tell: Joe, Thad,
Meenie, Kenny, Franny, Tubby, Stubby, Kenway, JD, dVoot, Corey, and Francine.

To Denny Crane, for raising awareness of Mad Cow disease.

Hmm, who am I forgetting? Oh yeah, and to my wife and kids.


You guys make life worth living.
Contents at a Glance

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Illustrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

PART 1 ■■■ Say Hello to My Little Friend:


JavaScript!
■CHAPTER 1 A Brief History of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
■CHAPTER 2 The Seven Habits of Highly Successful
JavaScript Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

PART 2 ■■■ The Projects


■CHAPTER 3 Hodgepodge: Building an Extensible JavaScript Library . . . . . . . . 71
■CHAPTER 4 CalcTron 3000: A JavaScript Calculator . . . . . . . . . . . . . . . . . . . . . . 107
■CHAPTER 5 Doing the Monster Mash: A Mashup . . . . . . . . . . . . . . . . . . . . . . . . . 147
■CHAPTER 6 Don’t Just Live in the Moment: Client-Side Persistence . . . . . . . 185
■CHAPTER 7 JSDigester: Taking the Pain Out of Client-Side XML . . . . . . . . . . . 231
■CHAPTER 8 Get It Right, Bub: A JavaScript Validation Framework . . . . . . . . . 261
■CHAPTER 9 Widget Mania: Using a GUI Widget Framework . . . . . . . . . . . . . . . 305
■CHAPTER 10 Shopping in Style: A Drag-and-Drop Shopping Cart . . . . . . . . . . . 351
■CHAPTER 11 Time for a Break: A JavaScript Game . . . . . . . . . . . . . . . . . . . . . . . . 403
■CHAPTER 12 Ajax: Where the Client and Server Collide . . . . . . . . . . . . . . . . . . . . 465

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525

v
Contents

About the Author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xv


About the Technical Reviewer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvii
About the Illustrator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxiii

PART 1 ■■■ Say Hello to My Little Friend:


JavaScript!
■CHAPTER 1 A Brief History of JavaScript ..............................3

How JavaScript Came to Exist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


The Evolution of JavaScript: Teething Pains . . . . . . . . . . . . . . . . . . . . . . . . 6
But It’s the Same Code: Browser Incompatibilities . . . . . . . . . . . . . . . 6
Of Snails and Elephants: JavaScript Performance and
Memory Issues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
The Root of All Evil: Developers! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
DHTML—The Devil’s Buzzword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
The Evolution Continues: Approaching Usability . . . . . . . . . . . . . . . . . . . . 18
Building a Better Widget: Code Structure . . . . . . . . . . . . . . . . . . . . . 19
Relearning Good Habits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
The Final Evolution: Professional JavaScript at Last! . . . . . . . . . . . . . . . . 21
The Browsers Come Around . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Object-Oriented JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
“Responsible” JavaScript: Signs and Portents . . . . . . . . . . . . . . . . . 26
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

vii
viii ■C O N T E N T S

■CHAPTER 2 The Seven Habits of Highly Successful


JavaScript Developers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
More on Object-Oriented JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Simple Object Creation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Object Creation with JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
Class Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Prototypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Which Approach Should You Use? . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Benefits of Object-Orientation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Graceful Degradation and Unobtrusive JavaScript . . . . . . . . . . . . . . . . . . 35
Keep JavaScript Separate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Allow Graceful Degradation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Don’t Use Browser-Sniffing Routines . . . . . . . . . . . . . . . . . . . . . . . . . 39
Don’t Create Browser-Specific or Dialect-Specific JavaScript . . . . 40
Properly Scope Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
Don’t Use Mouse Events to Trigger Required Events . . . . . . . . . . . . 41
It’s Not All Just for Show: Accessibility Concerns . . . . . . . . . . . . . . . . . . . 42
When Life Gives You Grapes, Make Wine: Error Handling . . . . . . . . . . . . 43
When It Doesn’t Go Quite Right: Debugging Techniques . . . . . . . . . . . . . 46
Browser Extensions That Make Life Better . . . . . . . . . . . . . . . . . . . . . . . . 49
Firefox Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
IE Extensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
Maxthon Extension: DevArt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
JavaScript Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Prototype . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Dojo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Java Web Parts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Script.aculo.us . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Yahoo! User Interface Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
MochiKit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
Rico . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Mootools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
■C O N T E N T S ix

PART 2 ■■■ The Projects


■CHAPTER 3 Hodgepodge: Building an Extensible
JavaScript Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Bill the n00b Starts the Day . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Overall Code Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Creating the Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Building the jscript.array Package . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Building the jscript.browser Package . . . . . . . . . . . . . . . . . . . . . . . . . 78
Building the jscript.datetime Package . . . . . . . . . . . . . . . . . . . . . . . . 78
Building the jscript.debug Package . . . . . . . . . . . . . . . . . . . . . . . . . . 80
Building the jscript.dom Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
Building the jscript.form Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
Building the jscript.lang Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Building the jscript.math Package . . . . . . . . . . . . . . . . . . . . . . . . . . . 91
Building the jscript.page Package . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
Building the jscript.storage Package . . . . . . . . . . . . . . . . . . . . . . . . . 94
Building the jscript.string Package . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Testing All the Pieces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105

■CHAPTER 4 CalcTron 3000: A JavaScript Calculator . . . . . . . . . . . . . . . . . 107


Calculator Project Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . 107
A Preview of CalcTron . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Rico Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Dissecting the CalcTron Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
Writing calctron.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Writing CalcTron.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
Writing Classloader.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Writing Mode.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
Writing Standard.json and Standard.js . . . . . . . . . . . . . . . . . . . . . . . 131
Writing BaseCalc.json and BaseCalc.js . . . . . . . . . . . . . . . . . . . . . . 140
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146
x ■C O N T E N T S

■CHAPTER 5 Doing the Monster Mash: A Mashup . . . . . . . . . . . . . . . . . . . . 147


What’s a Mashup? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
Monster Mash(up) Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . 148
The Yahoo APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Yahoo Maps Map Image Service . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Yahoo Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
The Google APIs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Script.aculo.us Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
A Preview of the Monster Mash(up) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
Dissecting the Monster Mash(up) Solution . . . . . . . . . . . . . . . . . . . . . . . 161
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
Writing mashup.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Writing ApplicationState.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168
Writing Hotel.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Writing SearchFuncs.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170
Writing Masher.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 173
Writing CallbackFuncs.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
Writing MapFuncs.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Writing MiscFuncs.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

■CHAPTER 6 Don’t Just Live in the Moment:


Client-Side Persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 185
Contact Manager Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . 185
Dojo Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Dojo and Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
Dojo Widgets and Event System . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
Local Shared Objects and the Dojo Storage System . . . . . . . . . . . 190
A Preview of the Contact Manager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192
Dissecting the Contact Manager Solution . . . . . . . . . . . . . . . . . . . . . . . . 194
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
Writing dojoStyles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Writing index.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Writing goodbye.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207
Writing EventHandlers.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
Writing Contact.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
Writing ContactManager.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
Writing DataManager.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223
■C O N T E N T S xi

Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229


Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229

■CHAPTER 7 JSDigester: Taking the Pain Out of Client-Side XML . . . . 231

Parsing XML in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 231


JSDigester Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
How Digester Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Dissecting the JSDigester Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 237
Writing the Test Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
Understanding the Overall JSDigester Flow . . . . . . . . . . . . . . . . . . 244
Writing the JSDigester Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
Writing the Rules Classes Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

■CHAPTER 8 Get It Right, Bub: A JavaScript


Validation Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
JSValidator Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
How We Will Pull It Off . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
The Prototype Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
A Preview of JSValidator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
Dissecting the JSValidator Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 268
Writing index.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270
Writing jsv_config.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271
Writing JSValidatorObjects.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Writing JSValidator.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287
Writing JSValidatorBasicValidators.js . . . . . . . . . . . . . . . . . . . . . . . . 297
Writing DateValidator.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

■CHAPTER 9 Widget Mania: Using a GUI Widget Framework . . . . . . . . . 305

JSNotes Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305


The YUI Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
A Preview of JSNotes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 307
xii ■C O N T E N T S

Dissecting the JSNotes Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310


Writing index.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 311
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 313
Writing Note.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317
Writing JSNotes.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

■CHAPTER 10 Shopping in Style: A Drag-and-Drop Shopping Cart . . . . 351


Shopping Cart Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . . . 351
Graceful Degradation, or Working in the Stone Age . . . . . . . . . . . . . . . . 352
The MochiKit Library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355
The Mock Server Technique . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 357
A Preview of the Shopping Cart Application . . . . . . . . . . . . . . . . . . . . . . 359
Dissecting the Shopping Cart Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Writing index.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367
Writing main.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
Writing idX.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 373
Writing CatalogItem.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
Writing Catalog.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Writing CartItem.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Writing Cart.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385
Writing viewCart.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Writing checkout.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Writing mockServer.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

■CHAPTER 11 Time for a Break: A JavaScript Game . . . . . . . . . . . . . . . . . . . 403


K&G Arcade Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . 403
A Preview of the K&G Arcade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 405
Dissecting the K&G Arcade Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Writing index.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413
Writing GameState.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415
Writing globals.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
■C O N T E N T S xiii

Writing main.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417


Writing consoleFuncs.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424
Writing keyHandlers.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Writing gameFuncs.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
Writing MiniGame.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Writing Title.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
Writing GameSelection.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437
Writing CosmicSquirrel.js. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 440
Writing Deathtrap.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448
Writing Refluxive.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 456
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

■CHAPTER 12 Ajax: Where the Client and Server Collide . . . . . . . . . . . . . . 465


Chat System Requirements and Goals . . . . . . . . . . . . . . . . . . . . . . . . . . . 465
The “Classic” Web Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 466
Ajax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
The Ajax Frame of Mind . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Accessibility and Similar Concerns . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Ajax: A Paradigm Shift for Many . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
The “Hello World” of Ajax Examples. . . . . . . . . . . . . . . . . . . . . . . . . 474
JSON . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481
Mootools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483
A Preview of the Chat Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484
Dissecting the Chat Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486
Writing SupportChat.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 488
Writing ChatMessage.js . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497
Writing styles.css . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500
Writing index.htm and index_support.htm. . . . . . . . . . . . . . . . . . . . 501
Writing chat.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503
Writing goodbye.htm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Creating the Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508
Writing the Server Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509
Suggested Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523
Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 523

■INDEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 525
About the Author

■FRANK W. ZAMMETTI is a web architect specialist for a leading worldwide financial company by
day, and a PocketPC and open source developer by night. He is the founder and chief software
architect of Omnytex Technologies, a PocketPC development house.
Frank has more than 13 years of “professional” experience in the IT field, and over 12 more
of “amateur” experience. He began his nearly lifelong love of computers at age 7, when he became
one of four students chosen to take part in the school district’s pilot computer program. A year
later, he was the only participant left! The first computer Frank owned was a Timex Sinclair 1000,
in 1982, on which he wrote a program to look up movie times for all of Long Island (and without
the 16kb expansion module!). After that, he moved on to an Atari computer, and then a
Commodore 64, where he spent about four years doing nothing but assembly programming
(games mostly). He finally got his first IBM-compatible PC in 1987, and began learning the finer
points of programming (as they existed at that time!).
Frank has primarily developed web-based applications for about eight years. Before that,
he developed Windows-based client/server applications in a variety of languages. Frank holds
numerous certifications, including SCJP, MCSD, CNA, i-Net+, A+, CIW Associate, MCP, and
numerous BrainBench certifications. He is a contributor to a number of open source projects,
including DataVision, Struts, PocketFrog, and Jakarta Commons. In addition, Frank has started
two projects: Java Web Parts and The Struts Web Services Enablement Project. He also was one
of the founding members of a project that created the first fully functioning Commodore 64
emulator for PocketPC devices (PocketHobbit).
Frank has authored various articles on topics that range from integrating DataVision into
web applications to using Ajax in Struts-based applications, as well as a book on Ajax for Apress.
He is currently working on a new application framework specifically geared to creating next-
generation web applications.
Frank lives in the United States with his wife Traci, his two kids Andrew and Ashley, and his
dog Belle. And an assortment of voices in his head, but the pills are supposed to stop that.

xv
About the Technical Reviewer

■HERMAN VAN ROSMALEN works as a developer/software architect for De Nederlandsche Bank N.V.,
the central bank of the Netherlands. He has more than 20 years of experience in developing
software applications in a variety of programming languages. Herman has been involved in
building mainframe, PC, and client/server applications. For the past six years, however, he has
been involved mainly in building J2EE web-based applications. After working with Struts
(pre-1.0) for years, he got interested in Ajax and joined the Java Web Parts open source project
in 2005.
Herman lives in a small town, Pijnacker, in the Netherlands, with his wife Liesbeth and
their children, Barbara, Leonie, and Ramon.

xvii
About the Illustrator

■ANTHONY VOLPE did the illustrations for this book and the K&G Arcade game. He has worked on
several video games with author Frank Zammetti, including Invasion Trivia!, Io Lander, and
Ajax Warrior. Anthony lives in Collegeville, Pennsylvania, and works as a graphic designer and
front-end web developer. His hobbies include recording music, writing fiction, making video
games, and going to karaoke bars to make a spectacle of himself.

xix
Acknowledgments

M any people helped make this book a reality in one form or another, and some of them may
not even realize it! I’ll try to remember them all here, but chances are I haven’t, and I apologize
in advance.
First and foremost, I would like to thank everyone at Apress who made this book a reality.
This is my second go-round with you folks, and it was just as pleasurable an experience this
time as the first. Chris, Matt, Tracy, Marilyn, Laura, Tina, and all the rest, thank you!
A great deal of thanks goes to Herman van Rosmalen, one of my partners in crime on the
Java Web Parts project (http://javawebparts.sourceforge.net) project, and technical reviewer
for this book. I know you put in a lot of time and effort in keeping me honest, and I can’t tell you
how much I appreciate it! Now, let’s get back to work on JWP!
A big thanks must also go to Anthony Volpe, the fine artist who did the illustrations for
this book. He and I have been friends for about ten years now, and we have collaborated on a
number of projects, including three PocketPC games (check ’em out: http://www.omnytex.com),
as well as a couple of Flash games (http://www.planetvolpe.com/crackhead) and some web
cartoons (http://www.planetvolpe.com/du). He is a fantastic artist, as I’m sure you can see for
yourself, an incredibly creative person, and a good friend to boot.
I would also like to thank those that built some of the libraries used in this book, including
all the folks working on Dojo, Sam Stephenson (Prototype), Aaron Newton, Christophe Beyls,
and Valerio Proietti of the Mootools team; Bob Ippolito of MochiKit fame; all the YUI developers;
and everyone working on script.aculo.us and Rico.
Last but most definitely not least, I would like to thank everyone who bought this book! I
sincerely hope you have as much fun reading it as I did writing it, and I hope that you find it to
be worth your hard-earned dollars and that it proves to be an educational and eye-opening
experience.
As I said, I know I am almost certainly forgetting a boatload of people, so how about I just
thank the entire world and be done with it?!? In fact, if I had the technology, I’d be like Wowbagger
the Infinitely Prolonged, only with “Thanks!” instead of insults.
And on that note, let’s get to some code!

xxi
Introduction

S o there I was, just minding my own business, when along came a publisher asking me if I’d
be interested in writing a book on JavaScript. It seemed like a good thing to do at the time, so I
said yes.
I’m just kidding. No one asked me, I just showed up one day on the doorstep of Apress with
a manuscript and some puppy-dog eyes. I’m just kidding again.
Seriously though, JavaScript is one of those kids we all knew when we were young who start
out really ugly, but whom everyone wants as their beautiful date to the prom years later. Then
they go on to Yale, become a district attorney, and suddenly everyone realizes that they really
want to be with that person. Fortunately, unlike the DA, JavaScript doesn’t involve crimes and
misdemeanors, since you know you don’t have a chance any other way with the DA!
JavaScript has quickly become one of the most important topics in web development, one
that any self-respecting web developer can’t do without. With the advent of Ajax, which I’ll talk
about in this book, JavaScript has very quickly gone from something that can enhance a web
site a little to something used to build very serious, professional-quality applications. It’s no
longer a peripheral player; it’s a main focus nowadays.
There are plenty of books on JavaScript and plenty of how-to articles strewn across the
intrawebs, any of which can be of great help to you. Far harder to come by though are real,
substantial examples. Oh, you can get a lot of simplistic, artificial examples to be sure, but it’s
more difficult to find full-blown, real-world applications that you can examine. Many developers
learn best by tearing apart code, messing around with it a bit, and generally getting their hands
dirty with real, working bits. That’s why I wrote this book: to fill that gap.
In this book, you will find two chapters on some general JavaScript topics, including a brief
history of JavaScript, good coding habits, debugging techniques, tools, and more. From then
on, it’s ten chapters of nothing but projects! Each chapter will present a different application,
explain its inner workings, and offer some suggested exercises you can do to sharpen your skills
and further your learning. The projects run the gamut from generally useful (an extensible
calculator) to current ideas (a mashup) to just plain fun (a JavaScript game).
In the process, you will learn about a wide variety of topics, including debugging techniques,
various JavaScript libraries, and a few somewhat unique and useful approaches to coding. I
believe you will also find this to be an entertaining book, and in fact, one of the exercises I suggest
from the beginning is to try to pick out all the pop-culture references scattered all over the place
(try to place them without looking at the footnotes that accompany most, but not all!). I tried to
make this book like an episode of Gilmore Girls in that regard (and if you aren’t familiar with the
show, there’s your first pop-culture reference!).
So, enough babbling (for the time being anyway). You know what’s coming, so let’s stop
dropping hints about numbers, Dharma, and bizarre connections between characters (pop-
culture reference number 2!), and get on with the good stuff. Let’s get on with the show!

xxiii
xxiv ■I N T R O D U C T I O N

An Overview of This Book


This book is divided into two main parts. Part 1, “Say Hello to My Little Friend: JavaScript!,”
contains two chapters:

• Chapter 1 is a brief history of JavaScript, from its humble beginning to its current state
of acceptance.

• Chapter 2 goes into the techniques and approaches employed by modern-day “profes-
sional” JavaScript developers.

Part 2, “The Projects,” contains ten chapters:

• Chapter 3 starts you off with the first project: an extensible, packaged collection of
utility functions.

• Chapter 4 develops an extensible calculator and introduces the first JavaScript library, Rico.

• Chapter 5 introduces the concept of a mashup, one of the hottest topics going today, by
way of a working example using the very popular script.aculo.us library.

• Chapter 6 uses the Dojo library to deal with an issue that comes up frequently in JavaScript
development, that of client-side data persistence.

• Chapter 7 explores the very useful JSDigester component of the Java Web Parts project,
which allows you to parse XML and create JavaScript objects from it without tedious
coding on your part.

• Chapter 8 develops an extensible validation framework for doing client-side form valida-
tion in a purely declarative fashion.

• Chapter 9 introduces the Yahoo! User Interface Library and uses it to create a handy little
contact manager application.

• Chapter 10 uses the MochiKit library to develop a drag-and-drop shopping cart for
e-commerce applications.

• Chapter 11 is where we get into the fun stuff: a JavaScript game! And not a simple little
Tetris clone or tile-matching game, but something a fair bit more substantial.

• Chapter 12 is where we have an in-depth look at Ajax, perhaps the biggest reason JavaScript
has taken on a whole new level of importance in recent years, using the relatively new
Mootools library.

Obtaining This Book’s Source Code


All the examples in this book are freely available from the Source Code section of the Apress
web site. In fact, due to the nature of this book, you will absolutely have to download the source
code before you begin Chapter 3. To do so, visit http://www.apress.com, click the Source Code
link, and find Practical JavaScript, DOM Scripting, and Ajax Projects in the list. From this
book’s home page, you can download the source code as a zip file. The source code is organized
by chapter.
Obtaining Updates for This Book
Writing a book is a big endeavor—quite a bit bigger than many people think! Contrary to what
I claim in private to my friends, I am not perfect. I make my mistakes like everyone else. Not in
this book of course. Oh no, none at all.
Ahem . . .
Let me apologize in advance for any errors you may find in this book. Rest assured that
everyone involved has gone to extremes to ensure there are none, but let’s be real here. We’ve
all read technical books before, and we know that the cold, sharp teeth of reality bite every now
and again. I’m sorry, I’m sorry, I’m sorry!
A current errata list is available from this book’s home page on the Apress web site
(http://www.apress.com) along with information about how to notify us of any errors you may
find. This will usually involve some sort of telepathy, but my understanding is that Windows
Vista Service Pack 1 will include this feature, so rest easy my friends.

Contacting the Author


I very much would like to hear your questions and comments regarding this book’s content and
source code examples. Please do feel free to email me directly at [email protected]
(spammers will be hunted down by Sentinels and disposed of). I will reply to your inquiries as
soon as I can, but please remember, I do have a life (no, really, I do . . . OK, no I don’t), so I may
not be able to reply immediately.
Lastly, and most important, thank you for buying this book! I thank you, my wife thanks
you, my kids thank you, my kids’ orthodontist thanks you, my dog’s veterinarian thanks you,
my roofing contractor thanks you . . .
PART 1
■■■

Say Hello to My Little


Friend: JavaScript!

Eaten any good books lately?

Q (to Worf) in the Star Trek: The Next Generation episode, “Deja-Q”

The Internet? Is that thing still around?

Homer Simpson

Programming today is a race between software engineers striving to build bigger and
better idiot-proof programs, and the Universe trying to produce bigger and better idiots.
So far, the Universe is winning.

Rich Cook

The first 90% of the code accounts for the first 10% of the development time. The
remaining 10% of the code accounts for the other 90% of the development time.

Tom Cargill

There are only two kinds of programming languages: those people always bitch about
and those nobody uses.

Bjarne Stroustrup

There are only two industries that refer to their customers as ‘users.’

Edward Tufte
Random documents with unrelated
content Scribd suggests to you:
two wounded bears at the foot of the tree made night hideous with
their ravings.
Such a frightful bedlam of noises had never before been heard.
Balser was again loading his gun, hoping to finish the bears, when
he saw two lighted torches approaching along the path over which
he had just come, and as they came into view imagine his
consternation when he recognized the forms of Liney Fox and her
brother Tom. Tom carried his father’s gun, for Mr. Fox had gone to
Brookville, and Liney, in addition to her torch, carried Tom’s hatchet.
Liney and Tom were approaching rapidly, and Balser called out to
them to stop. They did not hear him, or did not heed him, but
continued to go forward to their death. The bears at the foot of the
tree were wounded, and would be more dangerous than even the
pack of wolves howling at the wagon.
“Go back! Go back!” cried Balser desperately, “or you’ll be killed. Two
wounded bears are at the root of the tree I’m in, and a hundred
wolves are howling in the hollow just below me. Run for your lives!
Run! You’ll be torn in pieces if you come here.”
The boy and girl did not stop, but continued to walk rapidly toward
the spot from which they had heard Balser call. The clouds had
drifted away from the moon, and now that the light was of little use
to Balser—for he was intent upon saving Liney and Tom—there was
plenty of it.
“... HE FELL A DISTANCE OF TEN OR TWELVE
FEET, ... AND LAY HALF STUNNED.”

The sound of his voice and the growling of the bears had attracted
the attention of the wolves. They were wavering in their attack upon
Mr. Brent, and evidently had half a notion to fall upon the bears that
Balser had wounded. Meantime Liney and Tom continued to
approach, and their torches, which under ordinary circumstances
would have frightened the animals away, attracted the attention of
the bears and the wolves, and drew the beasts upon them. They
were now within a few yards of certain death, and again Balser in
agony cried out: “Go back, Liney! Go back! Run for your lives!” In his
eagerness he rose to his feet, and took a step or two out upon the
rotten limb on which he had been seated. As he called to Liney and
Tom, and motioned to them frantically to go back, the limb upon
which he was standing broke, and he fell a distance of ten or twelve
feet to the ground, and lay half stunned between the two wounded
bears. Just as Balser fell, Liney and Tom came up to the rotten tree,
and at the same time the pack of wolves abandoned their attack
upon Mr. Brent and rushed like a herd of howling demons upon the
three helpless children.
One of the bears immediately seized Balser, and the other one struck
Liney to the ground. By the light of the torches Mr. Brent saw all that
had happened, and when the wolves abandoned their attack upon
him he hurried forward to rescue Balser, Liney, and Tom, although in
so doing he was going to meet his death. In a few seconds Mr. Brent
was in the midst of the terrible fight, and a dozen wolves sprang
upon him. Tom’s gun was useless, so he snatched the hatchet from
Liney, who was lying prostrate under one of the bears, and tried to
rescue her from its jaws. Had he done so, however, it would have
been only to save her for the wolves. But his attempt to rescue Liney
was quickly brought to an end. The wolves sprang upon Tom, and
soon he, too, was upon the ground. The resinous torches which had
fallen from the hands of Tom and Liney continued to burn, and cast
a lurid light upon the terrible scene.
Consciousness soon returned to Balser, and he saw with horror the
fate that was in store for his father, his friends, and himself. Despair
took possession of his soul, and he knew that the lamp of life would
soon be black in all of them forever. While his father and Tom lay
upon the ground at the mercy of the wolves, and while Liney was
lying within arm’s reach of him in the jaws of the wounded bear, and
he utterly helpless to save the girl of whom he was so fond, Balser’s
mother shook him by the shoulder and said, “Balser, your father is
coming.” Balser sprang to his feet, looked dazed for a moment, and
then ran, half weeping, half laughing, into his father’s arms ... just
as the sleepy little clock had finished striking nine.
CHAPTER X.
A CASTLE ON THE BRANDYWINE.

Christmas morning the boys awakened early and crept from beneath
their warm bearskins in eager anticipation of gifts from Santa Claus.
Of course they had long before learned who Santa Claus was, but
they loved the story, and in the wisdom of their innocence clung to
an illusion which brought them happiness.
The sun had risen upon a scene such as winter only can produce.
Surely Aladdin had come to Blue River upon the wings of the
Christmas storm, had rubbed his lamp, and lo! the humble cabin was
in the heart of a fairyland such as was never conceived by the mind
of a genie. Snow lay upon the ground like a soft carpet of white
velvet ten inches thick. The boughs of the trees were festooned with
a foliage that spring cannot rival. Even the locust trees, which in
their pride of blossom cry out in June time for our admiration,
seemed to say, “See what we can do in winter;” and the sycamore
and beech drooped their branches, as if to call attention to their
winter flowers given by that rarest of artists, Jack Frost.
The boys quickly donned their heavy buckskin clothing and
moccasins, and climbed down the pole to the room where their
father and mother were sleeping. Jim awakened his parents with a
cry of “Christmas Gift,” but Balser’s attention was attracted to a
barrel standing by the fireplace, which his father had brought from
Brookville, and into which the boys had not been permitted to look
the night before. Balser had a shrewd suspicion of what the barrel
contained, and his delight knew no bounds when he found, as he
had hoped, that it was filled with steel traps of the size used to catch
beavers, coons, and foxes.
Since he had owned a gun, Balser’s great desire had been to
possess a number of traps. As I have already told you, the pelts of
animals taken in winter are of great value, and our little hero longed
to begin life on his own account as a hunter and trapper.
I might tell you of the joyous Christmas morning in the humble cabin
when the gifts which Mr. Brent had brought from Brookville were
distributed. I might tell you of the new gown for mother, of the
bright, red mufflers, of the shoes for Sunday wear and the “store”
caps for the boys, to be used upon holiday occasions. I might tell
you of the candies and nuts, and of the rarest of all the gifts, an
orange for each member of the family, for that fruit had never before
been seen upon Blue River. But I must take you to the castle on
Brandywine.
You may wonder how there came to be a castle in the wilderness on
Brandywine, but I am sure, when you learn about it, you will declare
that it was fairer than any castle ever built of mortar and stone, and
that the adventures which befell our little heroes were as glorious as
ever fell to the lot of spurred and belted knight.
Immediately after breakfast, when the chores had all been finished,
Balser and Jim started down the river to visit Liney and Tom. Balser
carried with him two Christmas presents for his friends—a steel trap
for Tom, and the orange which his father had brought him from
Brookville for Liney.
I might also tell you of Tom’s delight when he received the trap, and
of Liney’s smile of pleasure, worth all the oranges in the world, when
she received her present; and I might fell you how she divided the
orange into pieces, and gave one to each of the family; and how,
after it had all been eaten, tears came to her bright eyes when she
learned that Balser had not tasted the fruit. I might tell you much
more that would be interesting, and show you how good and true
and gentle were these honest, simple folk, but I must drop it all and
begin my story.
Balser told Tom about the traps, and a trapping expedition was
quickly agreed upon between the boys.
The next day Tom went to visit Balser, and for three or four days the
boys were busily engaged in making two sleds upon which to carry
provisions for their campaign. The sleds when finished were each
about two feet broad and six feet long. They were made of elm, and
were very strong, and were so light that when loaded the boys could
easily draw them over the snow. By the time the sleds were finished
the snow was hard, and everything was ready for the moving of the
expedition.
First, the traps were packed. Then provisions, consisting of sweet
potatoes, a great lump of maple sugar, a dozen loaves of white
bread, two or three gourds full of butter, a side of bacon, a bag of
meal, a large piece of bear meat for the dogs, and a number of
other articles and simple utensils such as the boys would need in
cooking, were loaded upon the sleds. They took with them no meat
other than bacon and the bear meat for the dogs, for they knew
they could make traps from the boughs of trees in which they could
catch quail and pheasants, and were sure to be able, in an hour’s
hunting, to provide enough venison to supply their wants for a much
longer time than they would remain in camp. There were also wild
turkeys to be killed, and fish to be caught through openings which
the boys would make in the ice of the creek.
Over the loaded sleds they spread woolly bearskins to be used for
beds and covering during the cold nights, and they also took with
them a number of tanned deerskins, with which to carpet the floor
of their castle and to close its doors and windows. Tom took with
him his wonderful hatchet, an axe, and his father’s rifle. Axe,
hatchets, and knives had been sharpened, and bullets had been
moulded in such vast numbers that one would have thought the
boys were going to war. Powder horns were filled, and a can of that
precious article was placed carefully upon each of the sleds.
Bright and early one morning Balser, Tom, and Jim, and last, but by
no means least, Tige and Prince, crossed Blue River, and started in a
northwestern direction toward a point on Brandywine where a
number of beaver dams were known to exist, ten miles distant from
the Brent cabin.

EN ROUTE FOR THE CASTLE.

Tom and Tige drew one of the sleds, and Balser and Prince drew the
other. During the first part of the trip, Jim would now and then lend
a helping hand, but toward the latter end of the journey he said he
thought it would be better for him to ride upon one of the sleds to
keep the load from falling off. Balser and Tom, however, did not
agree with him, nor did the dogs; so Jim walked behind and
grumbled, and had his grumbling for his pains, as usually is the case
with grumblers.
Two or three hours before sunset the boys reached Brandywine, a
babbling little creek in springtime, winding its crooked rippling way
through overhanging boughs of water elm, sycamore, and willows,
but, at the time of our heroes’ expedition, frozen over with the mail
of winter. It is in small creeks, such as Brandywine, that beavers love
to make their dams.
Our little caravan, upon reaching Brandywine, at once took to the ice
and started up-stream along its winding course.
Jim had grown tired. “I don’t believe you fellows know where you’re
going,” said he. “I don’t see any place to camp.”
“You’ll see it pretty quickly,” said Balser; and when they turned a
bend in the creek they beheld a huge sycamore springing from a
little valley that led down to the water’s edge.
“There’s our home,” said Balser.
The sycamore was hollow, and at its roots was an opening for a
doorway.
Upon beholding the tree Jim gave a cry of delight, and was for
entering their new home at once, but Balser held him back and sent
in the dogs as an exploring advance guard. Soon the dogs came out
and informed the boys that everything within the tree was all right,
and Balser and Tom and Jim stooped low and entered upon the
possession of their castle on Brandywine.
The first task was to sweep out the dust and dry leaves. This the
boys did with bundles of twigs rudely fashioned into brooms. The
dry leaves and small tufts of black hair gave evidence all too strongly
that the castle which the boys had captured was the home of some
baron bear who had incautiously left his stronghold unguarded. Jim
spoke of this fact with unpleasant emphasis, and was ready to “bet”
that the bear would come back when they were all asleep, and
would take possession of his castle and devour the intruders.
“What will you bet?” said Tom.
“I didn’t say I would bet anything. I just said I’d bet, and you’ll see
I’m right,” returned Jim.
Balser and Tom well knew that Jim’s prophecy might easily come
true, but they had faith in the watchfulness of their sentinels, Tige
and Prince, and the moon being at its full, they hoped rather than
feared that his bearship might return, and were confident that, in
case he did, his danger would be greater than theirs.
After the castle floor had been carefully swept, the boys carried in
the deerskins and spread them on the ground for a carpet. The
bearskins were then taken in, and the beds were made; traps, guns,
and provisions were stored away, and the sleds were drawn around
to one side of the door, and placed leaning against the tree.
The boys were hungry, and Jim insisted that supper should be
prepared at once; but Tom, having made several trips around the
tree, remarked mysteriously that he had a plan of his own. He said
there was a great deal of work to be done before sundown, and that
supper could be eaten after dark when they could not work. Tom
was right, for the night gave promise of bitter cold.
Limpy did not tell his plans at once, but soon they were developed.
The hollow in the tree in which the boys had made their home was
almost circular in form. It was at least ten or eleven feet in diameter,
and extended up into the tree twenty or thirty feet. Springing from
the same root, and a part of the parent tree, grew two large sprouts
or branches, which at a little distance looked like separate trees.
They were, however, each connected with the larger tree, and the
three formed one.
“What on earth are you pounding at that tree for?” asked Jim, while
Tom was striking one of the smaller trees with the butt end of the
hatchet, and listening intently as if he expected to hear a response.
Tom did not reply to Jim, but in a moment entered the main tree
with axe in hand, and soon Balser and Jim heard him chopping.
The two boys at once followed Tom, to learn what their eccentric
companion was doing. Tom did not respond to their questions, but
after he had chopped vigorously for a few minutes the result of his
work gave them an answer, for he soon cut an opening into the
smaller tree, which was also hollow. Tom had discovered the hollow
by striking the tree with his hatchet. In fact, Tom was a genius after
his own peculiar pattern.
The newly discovered hollow proved to be three or four feet in
diameter, and, like that in the larger tree, extended to a considerable
height. After Tom had made the opening between the trees, he sat
upon the ground, and with his hatchet hewed it to an oval shape,
two feet high and two feet broad.
Jim could not imagine why Tom had taken so much trouble to add
another room to their house, which was already large enough. But
when Tom, having finished the opening upon the inside, went out
and began to climb the smaller tree with the help of a few low-
growing branches, the youngest member of the expedition became
fully convinced in his own mind that the second in command was out
of his head entirely. When Tom, having climbed to a height of twelve
or fifteen feet, began to chop with his hatchet, Jim remarked, in
most emphatic language, that he thought “a fellow who would chop
at a sycamore tree just for the sake of making chips, when he might
be eating his supper, was too big a fool to live.”
Tom did not respond to Jim’s sarcasm, but persevered in his
chopping until he had made an opening at the point to which he had
climbed. Balser had quickly guessed the object of Tom’s mighty
labors, but he did not enlighten Jim. He had gone to other work, and
by the time Tom had made the opening from the outside of the
smaller tree, had collected a pile of firewood, and had carried
several loads of it into the castle. Then Tom came down, and Jim
quickly followed him into the large tree, for by that time his
mysterious movements were full of interest to the little fellow.
Now what do you suppose was Tom’s object in wasting so much
time and energy with his axe and hatchet?
A fireplace.
You will at once understand that the opening which Tom had cut in
the tree at the height of twelve or fifteen feet was for the purpose of
making a chimney through which the smoke might escape.
The boys kindled a fire, and in a few minutes there was a cheery
blaze in their fireplace that lighted up the room and made
“everything look just like home,” Jim said.
Then Jim went outside and gave a great hurrah of delight when he
saw the smoke issuing from the chimney that ingenious Tom had
made with his hatchet.
THE CASTLE ON THE
BRANDYWINE.

Jim watched the smoke for a few moments, and then walked around
the tree to survey the premises. The result of his survey was the
discovery of a hollow in the third tree of their castle, and when he
informed Balser and Tom of the important fact, it was agreed that
the room which Jim had found should be prepared for Tige and
Prince. The dogs were not fastidious, and a sleeping-place was soon
made for them entirely to their satisfaction.
Meantime the fire was blazing and crackling in the fireplace, and the
boys began to prepare supper. They had not had time to kill game,
so they fried a few pieces of bacon and a dozen eggs, of which they
had brought a good supply, and roasted a few sweet potatoes in the
ashes. Then they made an opening in the ice, from which they drew
a bucketful of sparkling ice water, and when all was ready they sat
down to supper, served with the rarest of all dressings, appetite
sauce, and at least one of the party, Jim, was happy as a boy could
be.
The dogs then received their supper of bear meat.
The members of the expedition, from the commanding officer Balser
to the high privates Tige and Prince, were very tired after their hard
day’s work, and when Tom and Balser showed the dogs their
sleeping-place, they curled up close to each other and soon were in
the land of dog dreams.
By the time supper was finished night had fallen, and while Tom and
Balser were engaged in stretching a deerskin across the door to
exclude the cold air, Jim crept between the bearskins and soon was
sound asleep, dreaming no doubt of suppers and dinners and
breakfasts, and scolding in his dreams like the veritable little
grumbler that he was. A great bed of embers had accumulated in
the fireplace, and upon them Balser placed a hickory knot for the
purpose of retaining fire till morning, and then he covered the fire
with ashes.
After all was ready Balser and Tom crept in between the bearskins,
and lying spoon-fashion, one on each side of Jim, lost no time in
making a rapid, happy journey to the land of Nod.
Tom slept next to the wall, next to Tom lay Jim, and next to Jim was
Balser. The boys were lying with their feet to the fire, and upon the
opposite side of the room was the doorway closed by the deerskin,
of which I have already told you.
Of course they went to bed “all standing,” as sailors say when they
lie down to sleep with their clothing on, for the weather was cold,
and the buckskin clothing and moccasins were soft and pleasant to
sleep in, and would materially assist the bearskins in keeping the
boys warm.
It must have been a pretty sight in the last flickering light of the
smouldering fire to see the three boys huddled closely together,
covered by the bearskins. I have no doubt had you seen them upon
that night they would have appeared to you like a sleeping bear. In
fact, before the night was over they did appear to—but I must not
go ahead of my story.
The swift-winged hours of darkness sped like moments to the
sleeping boys. The smouldering coals in the fireplace were black and
lustreless. The night wind softly moaned through the branches of
the sycamore, and sighed as it swept the bare limbs of the willows
and the rustling tops of the underbrush. Jack Frost was silently at
work, and the cold, clear air seemed to glitter in the moonlight. It
was an hour past midnight. Had the boys been awake and listening,
or had Tige and Prince been attending to their duties as sentinels,
they would have heard a crisp noise of footsteps, as the icy surface
of the snow cracked, and as dead twigs broke beneath a heavy
weight. Ah, could the boys but awaken! Could the dogs be aroused
but for one instant from their deep lethargy of slumber!
Balser! Tom! Jim! Tige! Prince! Awaken! Awaken!
On comes the heavy footfall, cautiously. As it approaches the castle
a few hurried steps are taken, and the black, awkward form lifts his
head and sniffs the air for signs of danger.
The baron has returned to claim his own, and Jim’s prophecy, at
least in part, has come true. The tracks upon the snow left by the
boys and dogs, and the sleds leaning against the tree, excite the
bear’s suspicion, and he stands like a statue for five minutes, trying
to make up his mind whether or not he shall enter his old domain.
The memory of his cozy home tempts him, and he cautiously walks
to the doorway of his house. The deerskin stretched across the
opening surprises him, and he carefully examines it with the aid of
his chief counsellor, his nose. Then he thrusts it aside with his head
and enters.
He sees the boys on the opposite side of the tree, and doubtless
fancies that his mate has gotten home before him, so he
complacently lies down beside the bearskins, and soon, he, too, is in
the land of bear dreams.
When a bear sleeps he snores, and the first loud snort from the
baron’s nostrils aroused Balser. At first Balser’s mind was in
confusion, and he thought that he was at home. In a moment,
however, he remembered where he was, and waited in the darkness
for a repetition of the sound that had awakened him. Soon it came
again, and Balser in his drowsiness fancied that Tom had changed
his place and was lying beside him, though never in all his life had
he heard such sounds proceed from Limpy’s nose. So he reached out
his hand, and at once was undeceived, for he touched the bear, and
at last Balser was awake. The boy’s hair seemed to stand erect upon
his head, and his blood grew cold in his veins, as he realized the
terrible situation. All was darkness. The guns, hatchets, and knives
were upon the opposite side of the tree, and to reach them or to
reach the doorway Balser would have to climb over the bear. Cold as
the night was, perspiration sprang from every pore of his skin, and
terror took possession of him such as he had never before known. It
seemed a long time that he lay there, but it could not have been
more than a few seconds until the bear gave forth another snort,
and Tom raised up from his side of the bed, and said: “Balser, for
goodness’ sake stop snoring. The noise you make would bring a
dead man to life.”
Tom’s voice aroused the bear, and it immediately rose upon its
haunches with a deep growl that seemed to shake the tree. Then
Jim awakened and began to scream. At the same instant Tige and
Prince entered the tree, and a fight at once ensued between the
bear and dogs. The bear was as badly frightened as the boys, and
when it and the dogs ran about the room the boys were thrown to
the ground and trampled upon.
The beast, in his desperate effort to escape, ran into the fireplace
and scattered the coals and ashes. As he could not escape through
the fireplace, he backed into the room, and again made the rounds
of the tree with the dogs at his heels. Again the boys were knocked
about as if they were ninepins. They made an effort to reach the
door, but all I have told you about took place so quickly, and the
darkness was so intense, that they failed to escape. Meantime the
fight between the dogs and the bear went on furiously, and the
barking, yelping, growling, and snarling made a noise that was
deafening. Balser lifted Jim to his arms and tried to save him from
injury, but his efforts were of small avail, for with each plunge of the
bear the boys were thrown to the ground or dashed against the tree,
until it seemed that there was not a spot upon their bodies that was
not bruised and scratched. At last, after a minute or two of awful
struggle and turmoil—a minute or two that seemed hours to the
boys—the bear made his exit through the door followed closely by
Tige and Prince, who clung to him with a persistency not to be
shaken off.
You may be sure that the boys lost no time in making their exit also.
Their first thoughts, of course, were of each other, and when Balser
learned that Jim and Tom had received no serious injury, he quickly
turned his head in the direction whence the bear and dogs had
gone, and saw them at a point in the bend of the creek not fifty
yards away. The bear had come to bay, and the dogs were in front
of him, at a safe distance, barking furiously. Then Balser’s courage
returned, and he hastily went into the tree, brought out his carbine,
and hurried toward the scene of conflict. The moon was at its full,
and the snow upon the trees and upon the ground helped to make
the night almost as light as day. The bear was sitting erect upon his
haunches, hurling defiant growls at the dogs, and when Balser
approached him, the brute presented his breast as a fair mark. Tom
also fetched his gun and followed closely at Balser’s heels. The
attention of the bear was so occupied with the dogs that he gave no
heed to the boys, and they easily approached him to within a
distance of five or six yards. Tom and Balser stood for a moment or
two with their guns ready to fire, and Balser said: “Tom, you shoot
first. I’ll watch carefully, and hold my fire until the bear makes a
rush, should you fail to kill him.”
Much to Balser’s surprise, Tom quickly and fearlessly took three or
four steps toward the bear, and when he lifted his father’s long gun
to fire, the end of it was within three yards of the bear’s breast.
“BALSER HESITATED TO FIRE, FEARING
THAT HE MIGHT KILL TOM OR ONE OF THE
DOGS.”

Balser held his ground, much frightened at Tom’s reckless bravery,


but did not dare to speak. When Tom fired, the bear gave forth a
fearful growl, and sprang like a wildcat right upon the boy. Tom fell
to the ground upon his back, and the bear stood over him. The dogs
quickly made an attack, and Balser hesitated to fire, fearing that he
might kill Tom or one of the dogs. Then came Jim, who rushed past
Balser toward Tom and the bear, and if Jim’s courage had ever
before been doubted, all such doubts were upon that night removed
forever. The little fellow carried in his hand Tom’s hatchet, and
without fear or hesitancy he ran to the bear and began to strike him
with all his little might. Meantime poor, prostrate Tom was crying
piteously for help, and, now that Jim was added to the group, it
seemed impossible for Balser to fire at the bear. But no time was to
be lost. If Balser did not shoot, Tom certainly would be killed in less
than ten seconds. So, without stopping to take thought, and upon
the impulse of one of those rare intuitions under the influence of
which persons move so accurately, Balser lifted his gun to his
shoulder. He could see the bear’s head plainly as it swayed from side
to side, just over Tom’s throat, and it seemed that he could not miss
his aim. Almost without looking, he pulled the trigger. He felt the
rebound of the gun and heard the report breaking the heavy silence
of the night. Then he dropped the gun upon the snow and covered
his face with his hands, fearing to see the result of his shot. He
stood for a moment trembling. The dogs had stopped barking; the
bear had stopped growling; Jim had ceased to cry out; Tom had
ceased his call for help, and the deep silence rested upon Balser’s
heart like a load of lead. He could not take his hands from his face.
After a moment he felt Jim’s little hand upon his arm, and Tom said,
as he drew himself from beneath the bear, “Balser, there’s no man or
boy living but you that could have made that shot in the moonlight.”
Then Balser knew that he had killed the bear, and he sank upon the
snow and wept as if his heart would break.
Notwithstanding the intense cold, the excitement of battle had made
the boys unconscious of it, and Tom and Jim stood by Balser’s side
as he sat upon the snow, and they did not feel the sting of the night.
Poor little Jim, who was so given to grumbling, much to the surprise
of his companions fell upon his knees, and said, “Don’t cry, Balser,
don’t cry,” although the tears were falling over the little fellow’s own
cheeks. “Don’t cry any more, Balser, the bear is dead all over. I
heard the bullet whiz past my ears, and I heard it strike the bear’s
head just as plain as you can hear that owl hoot; and then I knew
that you had saved Tom and me, because nobody can shoot as well
as you can.”
The little fellow’s tenderness and his pride in Balser seemed all the
sweeter, because it sprang from his childish gruffness.
Tom and Jim helped Balser to his feet, and they went over to the
spot where the bear was lying stone dead with Balser’s bullet in his
brain. The dogs were sniffing at the dead bear, and the monster
brute lay upon the snow in the moonlight, and looked like a huge
incarnate fiend.
After examining him for a moment the boys slowly walked back to
the tree. When they had entered they raked the coals together, put
on an armful of wood, called in the dogs to share their comfort,
hung up the deerskin at the door, drew the bearskins in front of the
fire, and sat down to talk and think, since there was no sleep left in
their eyes for the rest of that night.
After a long silence Jim said, “I told you he’d come back.”
“But he didn’t eat us,” replied Tom, determined that Jim should not
be right in everything.
“He’d have eaten you, Limpy Fox, if Balser hadn’t been the best shot
in the world.”
“That’s what he would,” answered Tom, half inclined to cry.
“Nonsense,” said Balser, “anybody could have done it.”
“Well, I reckon not” said Jim. “Me and Tom and the dogs and the
bear was as thick as six in a bed; and honest, Balser, I think you had
to shoot around a curve to miss us all but the bear.”
After a few minutes Jim said: “Golly! wasn’t that an awful fight we
had in here before the bear got out?”
“Yes, it was,” returned Balser, seriously.
“Well, I rather think it was,” continued Jim. “Honestly, fellows, I ran
around this here room so fast for a while, that—that I could see my
own back most of the time.”
Balser and Tom laughed, and Tom said: “Jim, if you keep on
improving, you’ll be a bigger liar than that fellow in the Bible before
you’re half his age.”
Then the boys lapsed into silence, and the dogs lay stretched before
the fire till the welcome sun began to climb the hill of the sky and
spread his blessed tints of gray and blue and pink and red, followed
by the glorious flood of day.
After breakfast the boys skinned the bear and cut his carcass into
small pieces—that is, such portions of it as they cared to keep. They
hung the bearskin and meat upon the branches of their castle
beyond the reach of wolves and foxes, and they gave to Tige and
Prince each a piece of meat that made their sides stand out with
fulness.
The saving of the bear meat and skin consumed most of the
morning, and at noon the boys took a loin steak from the bear and
broiled it upon the coals for dinner. After dinner they began the real
work of the expedition by preparing to set the traps.
When all was ready they started up the creek, each boy carrying a
load of traps over his shoulder. At a distance of a little more than
half a mile from the castle they found a beaver dam stretching
across the creek, and at the water’s edge near each end of the dam
they saw numberless tracks made by the little animals whose
precious pelts they were so anxious to obtain.
I should like to tell you of the marvellous home of that wonderful
little animal the beaver, and of his curious habits and instincts; how
he chops wood and digs into the ground and plasters his home,
under the water, with mud, using his tail for shovel and trowel. But
all that you may learn from any book on natural history, and I assure
you it will be found interesting reading.
The boys placed five or six traps upon the beaver paths on each side
of the creek, and then continued their journey up stream until they
found a little opening in the ice down to which, from the bank
above, ran a well-beaten path, telling plainly of the many kinds of
animals that had been going there to drink. There they set a few
traps and baited them with small pieces of bear meat, and then they
returned home, intending to visit the traps next morning at an early
hour, and hoping to reap a rich harvest of pelts.
When the boys reached home it lacked little more than an hour of
sunset, but the young fellows had recovered from the excitement of
the night before, which had somewhat destroyed their appetites for
breakfast and dinner, and by the time they had returned from setting
their traps those same appetites were asserting themselves with a
vigour that showed plainly enough a fixed determination to make up
for lost time.
“How would a wild turkey or a venison steak taste for supper?”
asked Balser.
Jim simply looked up at him with a greedy, hungry expression, and
exclaimed the one word—“Taste?”
“Well, I’ll go down the creek a little way and see what I can find. You
fellows stay here and build a fire, so that we can have a fine bed of
coals when I return.”
Balser shouldered his gun and went down the creek to find his
supper. He did not take the dogs, for he hoped to kill a wild turkey,
and dogs are apt to bark in the pursuit of squirrels and rabbits,
thereby frightening the turkey, which is a shy and wary bird.
When the boy had travelled quite a long
distance down stream, he began to fear
that, after all, he should be compelled to
content himself with a rabbit or two for
supper. So he turned homeward and
scanned the woods carefully for the humble
game, that he might not go home entirely
empty-handed.
Upon his journey down the creek rabbits
had sprung up on every side of him, but
now that he wanted a pair for supper they
all had mysteriously disappeared, and he feared that he and the
boys and the dogs would be compelled to content themselves with
bear meat.
“ESPIED A DOE AND A FAWN,
STANDING UPON THE OPPOSITE
SIDE OF THE CREEK.”

When the boy was within a few hundred yards of home, and had
almost despaired of obtaining even a rabbit, he espied a doe and a
fawn, standing upon the opposite side of the creek at a distance of
sixty or seventy yards, watching him intently with their great brown
eyes, so full of fatal curiosity. Balser imitated the cry of the fawn,
and held the attention of the doe until he was enabled to lessen the
distance by fifteen or twenty yards. Then he shot the fawn, knowing
that if he did so, its mother, the doe, would run for a short distance
and would return to the fawn. In the meantime Balser would load his
gun and would kill the doe when she returned. And so it happened
that the doe and the fawn each fell a victim to our hunter’s skill.
Balser threw the fawn over his shoulder and carried it to the castle;
then the boys took one of the sleds and fetched home the doe.
They hung the doe high upon the branches of the sycamore, and cut
the fawn into small pieces, which they put upon the ice of the creek
and covered with snow, that the meat might quickly cool. The bed of
coals was ready, and the boys were ready too, you may be sure.
Soon the fawn meat cooled, and soon each boy was devouring a
savoury piece that had been broiled upon the coals.
After supper the boys again built a fine fire, and sat before it talking
of the events of the day, and wondering how many beavers, foxes,
coons, and muskrats they would find in their traps next morning.
As the fire died down drowsiness stole over our trappers, who were
in the habit of going to bed soon after sunset, and they again crept
in between the bearskins with Jim in the middle. They, however,
took the precaution to keep Tige and Prince in the same room with
them, and the boys slept that night without fear of an intrusion such
as had disturbed them the night before.
Next morning, bright and early, the boys hurried up the creek to
examine their traps, and greatly to their joy found five beavers and
several minks, coons, and muskrats safely captured. Near one of the
traps was the foot of a fox, which its possessor had bitten off in the
night when he learned that he could not free it from the cruel steel.
The boys killed the animals they had caught by striking them on the
head with a heavy club, which method of inflicting death did not
damage the pelts as a sharp instrument or bullet would have done.
After resetting the traps, our hunters placed the game upon the sled
and hurried home to their castle, where the pelts were carefully
removed, stretched upon forked sticks, and hung up to dry.
Our heroes remained in camp for ten or twelve days, and each
morning brought them a fine supply of fur. They met with no other
adventure worthy to be related, and one day was like another. They
awakened each morning with the sun, and ate their breakfast of
broiled venison, fish, or quail, with now and then a rabbit. Upon one
occasion they had the breast of a wild turkey. They sought the traps,
took the game, prepared the pelts, ate their dinners and suppers of
broiled meats and baked sweet potatoes, and slumbered cozily
beneath their warm bearskins till morning.
One day Balser noticed that the snow was melting and was falling
from the trees. He and his companions had taken enough pelts to
make a heavy load upon each of the sleds. They feared that the
weather might suddenly grow warm and that the snow might
disappear. So they leisurely packed the pelts and their belongings,
and next morning started for home on Blue River, the richest,
happiest boys in the settlement.
They were glad to go home, but it was with a touch of sadness,
when they passed around the bend in the creek, that they said
“Good-by” to their “Castle on Brandywine.”
Dorothy Vernon of Haddon Hall

By CHARLES MAJOR

Author of “When Knighthood Was in Flower,” etc.

With eight full-page illustrations by Howard Chandler Christy

Cloth 12mo $1.50

“Dorothy Vernon is an Elizabethan maid, but a living, loving, lovable


girl.... The lover of accuracy of history in fiction may rest contented,
with the story; but he will probably care little for that once he has
been caught by the spirit and freshness of the romance.”—The Mail
and Express.
“Dorothy is a splendid creation, a superb creature of brains, beauty,
force, capacity, and passion, a riot of energy, love, and red blood.
She is the fairest, fiercest, strongest, tenderest heroine that ever
woke up a jaded novel reader and made him realize that life will be
worth living so long as the writers of fiction create her like.... The
story has brains, ‘go,’ virility, gumption, and originality.”—The Boston
Transcript.
“Dorothy is a fascinating character, whose womanly whims and
cunning ways in dealing with her manly, honest lover and her
wrathful father are cleverly portrayed. The interest is maintained to
the end. Some might call Dorothy a vixen, but she is of that rare and
ravishing kind who have tried (and satisfied) men’s souls from the
days of Mother Eve to the present time.”—The New York Herald.
“A romance of much delicacy, variety, strength, and grace, in which
are revealed the history of four lovers who by their purely human
attributes are distinct types.”—Evening Journal News, Evansville.
“As a study of woman, the incomprehensible, yet thoroughly lovable,
Dorothy Vernon clearly leads all recent attempts in fiction. Dorothy is
a wonderful creature.”—Columbus Evening Dispatch.
“Dorothy is a feminine whirlwind, very attractive to her audience if
somewhat disconcerting to her victims, and the story, even in these
days when romance has become a drug, makes good reading.”—
New York Life.

Tommy-Anne and the Three Hearts

By MABEL OSGOOD WRIGHT

Author of “Dogtown,” “Citizen Bird,” etc.

With illustrations by Albert Blashfield

Cloth 12mo $1.50

“The more of such books as these, the better for the children. One
Tommy-Anne is worth a whole shelf of the average juvenile
literature.”—The Critic.
“A better gift book for the little folks there could not be than this
charming work.... Genius of this rare order is decidedly one of Mrs.
Wright’s gifts, and this is proven by the fascinated interest that gray-
haired readers cannot help feeling in the book.”—The American,
Philadelphia.
“The child who reads will be charmed while he is instructed, and led
on to make new discoveries for himself.”—The Nation.

Wabeno, the Magician

The sequel to “Tommy-Anne and the Three Hearts”

By MABEL OSGOOD WRIGHT

Author of “Four-footed Americans,” etc.

With illustrations by Joseph M. Gleeson

Cloth 12mo $1.50

“Only positive genius could weave such subtle webs of fancy,


poetical in warp and woof, yet practical in knowledge. The book is
interestingly illustrated.”—The Chautauquan.

THE MACMILLAN COMPANY

64–66 FIFTH AVENUE, NEW YORK


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