100% found this document useful (2 votes)
36 views

JavaScript A Beginner s Guide Third Edition John Pollock - The complete ebook version is now available for download

The document provides information about downloading 'JavaScript: A Beginner's Guide, Third Edition' by John Pollock, along with links to other recommended ebooks and textbooks. It includes details about the author, technical editor, and copyright information. The content outlines various topics covered in the book, such as JavaScript fundamentals, functions, and advanced techniques.

Uploaded by

atildamagoss
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 (2 votes)
36 views

JavaScript A Beginner s Guide Third Edition John Pollock - The complete ebook version is now available for download

The document provides information about downloading 'JavaScript: A Beginner's Guide, Third Edition' by John Pollock, along with links to other recommended ebooks and textbooks. It includes details about the author, technical editor, and copyright information. The content outlines various topics covered in the book, such as JavaScript fundamentals, functions, and advanced techniques.

Uploaded by

atildamagoss
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/ 57

Visit ebookfinal.

com to download the full version and


explore more ebooks or textbooks

JavaScript A Beginner s Guide Third Edition John


Pollock

_____ Click the link below to download _____


https://ebookfinal.com/download/javascript-a-beginner-s-
guide-third-edition-john-pollock/

Explore and download more ebooks or textbook at ebookfinal.com


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

A Beginner s Guide To BorderManager 3 x Third Edition


Revision 1 Anonymous

https://ebookfinal.com/download/a-beginner-s-guide-to-
bordermanager-3-x-third-edition-revision-1-anonymous/

Network Security A Beginner s Guide Second Edition


Beginner s Guide Eric Maiwald

https://ebookfinal.com/download/network-security-a-beginner-s-guide-
second-edition-beginner-s-guide-eric-maiwald/

Javascript Absolute Beginners Guide Third Edition Kirupa


Chinnathambi

https://ebookfinal.com/download/javascript-absolute-beginners-guide-
third-edition-kirupa-chinnathambi/

Learning Web Design A Beginner s Guide to HTML CSS


JavaScript and Web Graphics 4th Edition Jennifer Niederst
Robbins
https://ebookfinal.com/download/learning-web-design-a-beginner-s-
guide-to-html-css-javascript-and-web-graphics-4th-edition-jennifer-
niederst-robbins/
Blender Game Engine Beginner s Guide Beginner s Guide 1st
ed Edition Bacone

https://ebookfinal.com/download/blender-game-engine-beginner-s-guide-
beginner-s-guide-1st-ed-edition-bacone/

The Brain A Beginner s Guide Oneworld Beginner s Guides


1st Edition Ammar Al-Chalabi

https://ebookfinal.com/download/the-brain-a-beginner-s-guide-oneworld-
beginner-s-guides-1st-edition-ammar-al-chalabi/

Databases A Beginner s Guide 1st Edition Andy Oppel

https://ebookfinal.com/download/databases-a-beginner-s-guide-1st-
edition-andy-oppel/

Networking A Beginner s Guide Fifth Edition Bruce Hallberg

https://ebookfinal.com/download/networking-a-beginner-s-guide-fifth-
edition-bruce-hallberg/

HTML A Beginner s Guide 2nd Edition Wendy Willard

https://ebookfinal.com/download/html-a-beginner-s-guide-2nd-edition-
wendy-willard/
JavaScript A Beginner s Guide Third Edition John
Pollock Digital Instant Download
Author(s): John Pollock
ISBN(s): 9780071632959, 0071632956
Edition: 3
File Details: PDF, 6.29 MB
Year: 2009
Language: english
JavaScript
A Beginner’s Guide

Third Edition
About the Author
John Pollock is employed as a Web Administrator during
the day and works on Web sites and other projects during the
evening. He runs two Web sites devoted to Web development
and design—PageResource.com (www.pageresource.com)
is a development tutorial site, and JavaScript City (www
.javascriptcity.com) is a site that offers free JavaScript code
to Web developers. John holds a bachelor of arts in English
from Sam Houston State University and currently lives in New
Waverly, Texas with his wife Heather.

About the Technical Editor


Scott Duffy is an author and consultant based in Toronto,
Canada. He designs and develops Web sites for small and
medium-sized companies.
JavaScript
A Beginner’s Guide

Third Edition
John Pollock

New York Chicago San Francisco


Lisbon London Madrid Mexico City
Milan New Delhi San Juan
Seoul Singapore Sydney Toronto
Copyright © 2010 by The McGraw-Hill Companies. All rights reserved. Except as permitted under the United States Copyright Act
of 1976, no part of this publication may be reproduced or distributed in any form or by any means, or stored in a database or retrieval
system, without the prior written permission of the publisher.

ISBN: 978-0-07-163296-6
MHID: 0-07-163296-4

The material in this eBook also appears in the print version of this title: ISBN: 978-0-07-163295-9, MHID: 0-07-163295-6.

All trademarks are trademarks of their respective owners. Rather than put a trademark symbol after every occurrence of a trad marked
name, we use names in an editorial fashion only, and to the benefit of the trademark owner, with no intention of infring ment of the
trademark. Where such designations appear in this book, they have been printed with initial caps.

McGraw-Hill eBooks are available at special quantity discounts to use as premiums and sales promotions, or for use in corporate
training programs. To contact a representative please e-mail us at bulksales@mcgraw-hill.com.

Information has been obtained by McGraw-Hill from sources believed to be reliable. However, because of the possibility of human
or mechanical error by our sources, McGraw-Hill, or others, McGraw-Hill does not guarantee the accuracy, adequacy, or complete-
ness of any information and is not responsible for any errors or omissions or the results obtained from the use of such information.

TERMS OF USE

This is a copyrighted work and The McGraw-Hill Companies, Inc. (“McGraw-Hill”) and its licensors reserve all rights in and to the
work. Use of this work is subject to these terms. Except as permitted under the Copyright Act of 1976 and the right to store and
retrieve one copy of the work, you may not decompile, disassemble, reverse engineer, reproduce, modify, create derivative works
based upon, transmit, distribute, disseminate, sell, publish or sublicense the work or any part of it without McGraw-Hill’s prior
copsent. You may use the work for your own noncommercial and personal use; any other use of the work is strictly prohibited. Your
right to use the work may be terminated if you fail to comply with these terms.

THE WORK IS PROVIDED “AS IS.” McGRAW-HILL AND ITS LICENSORS MAKE NO GUARANTEES OR WARRANTIES
AS TO THE ACCURACY, ADEQUACY OR COMPLETENESS OF OR RESULTS TO BE OBTAINED FROM USING THE
WORK, INCLUDING ANY INFORMATION THAT CAN BE ACCESSED THROUGH THE WORK VIA HYPERLINK OR
OTHERWISE, AND EXPRESSLY DISCLAIM ANY WARRANTY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMIT-
ED TO IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. McGraw-Hill and
its licensors do not warrant or guarantee that the functions contained in the work will meet your requirements or that its operation
will be uninterrupted or error free. Neither McGraw-Hill nor its licensors shall be liable to you or anyone else for any inaccuracy,
error or omission, regardless of cause, in the work or for any damages resulting therefrom. McGraw-Hill has no responsibility for
the content of any information accessed through the work. Under no circumstances shall McGraw-Hill and/or its licensors be liable
for any indirect, incidental, special, punitive, consequential or similar damages that result from the use of or inability to use the work,
even if any of them has been advised of the possibility of such damages. This limitation of liability shall apply to any claim or cause
whatsoever whether such claim or cause arises in contract, tort or otherwise.
To my wife Heather Pollock, Bruce and Joy Anderson, and
Dr. J. D. and Linda Andrews

In memory of James D. and Livian Anderson, John William and Edith Hopkins,
Burley T. and Aline Price, and “Doc” Flores
This page intentionally left blank
Contents at a Glance
1 Introduction to JavaScript ................................................ 1
2 Placing JavaScript in an HTML File ...................................... 15
3 Using Variables ........................................................... 33
4 Using Functions ........................................................... 59
5 JavaScript Operators ..................................................... 87
6 Conditional Statements and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
7 Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
8 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
9 The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
10 Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 241
11 JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
12 Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305

vii
viii JavaScript: A Beginner’s Guide

13 Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341


14 JavaScript and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375
15 JavaScript and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407
16 An Introduction to Advanced Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435
A Answers to Self Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Contents
ACKNOWLEDGMENTS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xix
INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi

1 Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
What You Need to Know . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
Basic HTML and CSS Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Basic Text Editor and Web Browser Knowledge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
Which Version? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Remember, It’s Not Java . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Similarities to Other Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Beginning with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Object Based . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Client Side ................................................................... 8
Scripting Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Online Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Try This 1-1: Use JavaScript to Write Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

ix
x JavaScript: A Beginner’s Guide

2 Placing JavaScript in an HTML File ..................................... 15


Using the HTML Script Tags ....................................................... 16
Identifying the Scripting Language . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Calling External Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Using <noscript></noscript> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Creating Your First Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Writing a “Hello World” Script ............................................... 19
Creating an HTML Document for the Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Inserting the Script into the HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Try This 2-1: Insert a Script into an HTML Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Using External JavaScript Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Creating a JavaScript File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Creating the HTML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
Viewing the Pages in Your Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Try This 2-2: Call an External Script from an HTML Document . . . . . . . . . . . . . . . . . . . . . 26
Using JavaScript Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Inserting Comments on One Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Adding Multiple-Line Comments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
3 Using Variables ........................................................... 33
Understanding Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
Why Variables Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Variables as Placeholders for Unknown Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Variables as Time-Savers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
Variables as Code Clarifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Defining Variables for Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Declaring Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Assigning Values to Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Naming Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
Understanding Variable Types ...................................................... 40
Number ...................................................................... 40
String . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Boolean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Null . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
Try This 3-1: Declare Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
Using Variables in Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
Making a Call to a Variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Adding Variables to Text Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Writing a Page of JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Creating the Framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Defining the Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
Adding the Commands ....................................................... 52
Modifying the Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
Try This 3-2: Create an HTML Page with JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
Contents xi

4 Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59
What a Function Is . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Why Functions Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
Structuring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Declaring Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Defining the Code for Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
Naming Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
Adding Parameters to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
Adding Return Statements to Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
Calling Functions in Your Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
Script Tags: Head Section or Body Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
Calling a Function from Another Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
Calling Functions with Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
Calling Functions with Return Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
Other Ways to Define Functions .............................................. 76
Try This 4-1: Create an HTML Page with Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
Putting It All Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
Try This 4-2: Write Your Own Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
5 JavaScript Operators ..................................................... 87
Understanding the Operator Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
Understanding Mathematical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
The Addition Operator (+) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
The Subtraction Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
The Multiplication Operator (*) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
The Division Operator (/) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
The Modulus Operator (%) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The Increment Operator (++) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
The Decrement Operator (– –) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 95
The Unary Negation Operator (–) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Understanding Assignment Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
The Assignment Operator (=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
The Add-and-Assign Operator (+=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
The Subtract-and-Assign Operator (–=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Multiply-and-Assign Operator (*=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Divide-and-Assign Operator (/=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
The Modulus-and-Assign Operator (%=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99
Try This 5-1: Adjust a Variable Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100
Understanding Comparison Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
The Is-Equal-To Operator (==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
The Is-Not-Equal-To Operator (!=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
The Is-Greater-Than Operator (>) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
The Is-Less-Than Operator (<) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104
xii JavaScript: A Beginner’s Guide

The Is-Greater-Than-or-Equal-To Operator (>=) .............................. 104


The Is-Less-Than-or-Equal-To Operator (<=) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The Strict Is-Equal-To Operator (===) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
The Strict Is-Not-Equal-To Operator (!==) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
Understanding Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The AND Operator (&&) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The OR Operator (||) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
The NOT Operator (!) ........................................................ 108
The Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
Special Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
Understanding Order of Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
Try This 5-2: True or False? ........................................................ 111
6 Conditional Statements and Loops ....................................... 115
Defining Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
What Is a Conditional Statement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
Why Conditional Statements Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Using Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Using if/else Statement Blocks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
Using the switch Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
Using the Conditional Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
Try This 6-1: Construct an if/else Block . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
Defining Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
What Is a Loop? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Why Loops Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
Using Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
do while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
for in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
for each in . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
Using break and continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
Try This 6-2: Work with for Loops and while Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
7 Event Handlers ........................................................... 147
What Is an Event Handler? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Why Event Handlers Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
Understanding Event Handler Locations and Uses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
Using an Event Handler in an HTML Element ................................ 149
Using an Event Handler in the Script Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151
Try This 7-1: Create a Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
Learning the Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 154
The Abort Event (onabort) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155
The Blur Event (onblur) ...................................................... 155
Contents xiii

The Change Event (onchange) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 156


The Click Event (onclick) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
The Focus Event (onfocus) ................................................... 158
The Keydown Event (onkeydown) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159
The Keypress Event (onkeypress) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
The Keyup Event (onkeyup) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
The Load Event (onload) ..................................................... 160
The Mousedown Event (onmousedown) ...................................... 161
The Mousemove Event (onmousemove) ...................................... 161
The Mouseover Event (onmouseover) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
The Mouseout Event (onmouseout) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163
The Mouseup Event (onmouseup) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
The Reset Event (onreset) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
The Submit Event (onsubmit) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
The Unload Event (onunload) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
Try This 7-2: Use Events to Send Out Alerts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Creating Scripts Using Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Text Box Message . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167
The Button Link . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169
Other Ways to Register Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
The addEventListener() Method .............................................. 172
The attachEvent() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
8 Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175
Defining Objects ................................................................... 176
What Is an Object? ........................................................... 176
Why Objects Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Creating Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Naming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
Object Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
Adding Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
Object Manipulation Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 190
Try This 8-1: Create a Computer Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193
Understanding Predefined JavaScript Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
The Navigator Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
The History Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
Try This 8-2: Practice with the Predefined Navigator Object . . . . . . . . . . . . . . . . . . . . . . . . . 201
9 The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
Defining the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
Using the Document Object Model ................................................. 206
Using the Properties of the Document Object ....................................... 207
The Color Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
The anchors Property (Array) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
xiv JavaScript: A Beginner’s Guide

The cookie Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210


The dir Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
The domain Property ......................................................... 212
The formname Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
The forms Property (Array) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
The images Property (Array) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
The lastModified Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
The layers Property (Array) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
The all Property .............................................................. 218
The links Property (Array) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
The referrer Property ......................................................... 219
The title Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
The URL Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
The URLUnencoded Property ................................................ 220
Using the Methods of the Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222
The getElementById() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224
The getElementsByClassName() Method ..................................... 224
The getElementsByTagName() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The open() and close() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 225
The write() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
The writeln() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 227
Creation Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228
Try This 9-1: Add a DOM Node to the Document . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233
Creating Dynamic Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Styles in JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 234
Coding a Dynamic Script . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235
The innerHTML Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236
Try This 9-2: Trying out Property Changes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 238
10 Window Object ........................................................... 241
An Introduction to the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242
Using the Properties of the Window Object ......................................... 242
The closed Property .......................................................... 243
The defaultStatus Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
The frames Property (Array) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
The innerHeight and innerWidth Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 244
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
The location Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246
The name Property ........................................................... 246
The opener Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247
The parent Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The self Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The status Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
The top Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 248
Contents xv

Try This 10-1: Use the location and innerWidth Properties .......................... 248
Using the Methods of the Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
The alert() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 249
The confirm() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 251
The find() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253
The home() Method .......................................................... 253
The print() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 254
The prompt() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255
The open() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256
The close() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261
The moveBy() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262
The moveTo() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263
The resizeBy() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The resizeTo() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The scrollBy() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The scrollTo() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265
The setInterval() Method ..................................................... 265
The clearInterval() Method ................................................... 266
The setTimeout() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
The clearTimeout() Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 267
Try This 10-2: Use the setTimeout() and confirm() Methods . . . . . . . . . . . . . . . . . . . . . . . . . 269
11 JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273
What Is an Array? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274
Why Arrays Are Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Defining and Accessing Arrays ..................................................... 275
Naming an Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275
Defining an Array ............................................................ 276
Accessing an Array’s Elements ............................................... 276
Other Ways to Define Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277
Understanding the Properties and Methods of the Array Object . . . . . . . . . . . . . . . . . . . . . . 279
Properties .................................................................... 279
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282
Extended Array Methods ..................................................... 291
Using Arrays with Loops ........................................................... 292
Creating Array Elements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292
Moving Through Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 293
Try This 11-1: Use Loops with Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297
Using Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 298
Defining Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Accessing Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299
Try This 11-2: Use Associative Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301
xvi JavaScript: A Beginner’s Guide

12 Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305


Using the Math Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
What Is the Math Object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
How the Math Object Is Useful . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 306
Properties .................................................................... 306
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308
Try This 12-1: Display a Random Link on a Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 321
Understanding the Number Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322
Properties .................................................................... 322
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324
Using the Date Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 326
Properties .................................................................... 326
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327
Methods That Get Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329
Methods That Set Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 332
Other Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333
How About Some Date Scripts? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 334
Try This 12-2: Create a JavaScript Clock . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338
13 Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341
Introduction to the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
The String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 342
The String Literal . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 343
What’s the Difference? ....................................................... 343
Using the Properties of the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
The constructor Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
The length Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 344
The prototype Property . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Using the Methods of the String Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
Methods That Add HTML Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345
The Other Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351
Try This 13-1: Use charAt() to Find a First Letter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359
Putting Methods Together . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360
Try This 13-2: Use indexOf() to Test an Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 362
Using Regular Expressions ......................................................... 363
Creating Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363
Testing Strings Against Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364
Adding Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365
Creating Powerful Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366
Grouping Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369
The replace(), match(), and search() Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370
More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 372
Contents xvii

14 JavaScript and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375


Accessing Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Using the forms Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376
Using Form Names . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380
Using an ID .................................................................. 381
Using the Properties and Methods of the Form Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382
Properties .................................................................... 382
Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Ensuring the Accessibility of Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 392
Using Proper Element and Label Order ....................................... 393
Using <label></label> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Using <fieldset></fieldset> Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 393
Not Assuming Client-Side Scripting .......................................... 394
Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
onsubmit and the return Statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395
Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 396
Try This 14-1: Request a Number . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398
Using Forms for Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Clicking a Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399
Try This 14-2: Build a Select Box Navigation Script ................................ 403
15 JavaScript and Frames ................................................... 407
An Introduction to Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
Purpose of Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408
The Code Behind the Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 409
Frame Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411
Accessing Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
The frames Array . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 414
Using a Frame Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 417
Changing Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Change a Single Frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418
Change Multiple Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419
Try This 15-1: Change Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Step by Step . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 422
Frame Navigation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 423
Using the Select Box with Frames ............................................ 423
Breaking Out of Frames ...................................................... 424
Sending Viewers to Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426
Using Variables Across Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 428
Try This 15-2: Use Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432
xviii JavaScript: A Beginner’s Guide

16 An Introduction to Advanced Techniques ................................ 435


Debugging Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436
JavaScript and Accessibility ........................................................ 442
Separate Content from Presentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 442
Enhancing Content ........................................................... 444
Try This 16-1: Make This Code Accessible . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445
Using Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Setting a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 446
Reading a Cookie . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449
Try This 16-2: Remember a Name . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451
Working with Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Preloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452
Rollovers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454
JavaScript Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Security and Signed Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
Page Protection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460
AJAX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 462
JavaScript Libraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463
A Answers to Self Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467
Chapter 1: Introduction to JavaScript . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Chapter 2: Placing JavaScript in an HTML File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468
Chapter 3: Using Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Chapter 4: Using Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 469
Chapter 5: JavaScript Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470
Chapter 6: Conditional Statements and Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Chapter 7: Event Handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 471
Chapter 8: Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 472
Chapter 9: The Document Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Chapter 10: Window Object . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473
Chapter 11: JavaScript Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474
Chapter 12: Math, Number, and Date Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Chapter 13: Handling Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475
Chapter 14: JavaScript and Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 476
Chapter 15: JavaScript and Frames . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477
Chapter 16: An Introduction to Advanced Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479
Acknowledgments
I would like to begin by thanking my wonderful wife, Heather Pollock, for all of her love,
support, and encouragement in all I do. I love you!
I would like to thank my parents, Bruce and Joy Anderson, for their love and guidance,
and for always supporting my endeavors.
I would like to thank Dr. J. D. and Linda Andrews for their love, guidance, and support.
In addition I would like to thank John and Betty Hopkins (grandparents), James D. and Livian
Anderson (grandparents), Clifton and Juanita Idom (grandparents), Richard Pollock (brother) and
family, Misty Castleman (sister) and family, Warren Anderson (brother) and family, Jon Andrews
(brother) and family, Lisa and Julian Owens (aunt/uncle) and family, and every aunt, uncle,
cousin, or other relation in my family. All of you have been a great influence in my life.
I would like to thank all of my editors at McGraw-Hill/Professional for their outstanding
help and support throughout the writing of this book. Thanks to Jane Brownlow, Joya Anthony,
Janet Walden, Smita Rajan, Bill McManus, Claire Splan, Jim Kussow, Jeff Weeks, and to all of
the copy editors who worked on each edition of the book.
Thanks to my technical editor, Scott Duffy, for editing and checking over all of the technical
aspects of the book, and helping me provide clear explanations of the topics that are covered.
I would like to thank my English professors at Sam Houston State University in Huntsville,
Texas for guiding me toward a better understanding of the English language. Thanks to James J.
Dent, Helena Halmari, Douglas Krienke, Julie Hall, Tracy Bilsing, Phillip Parotti, Ralph Pease,
Paul Ruffin, and Jack Kerr. In addition, I thank all of my other professors at the university for
helping me gain knowledge in so many areas.

xix
Discovering Diverse Content Through
Random Scribd Documents
thousand cavalry against Sus-al-Aska, the head-quarters of the
insurgents, soon had the satisfaction of learning that the rebellion
was subdued, and the recalcitrant Berbers punished with a rigor
unexampled even in the sanguinary wars of Africa. After making two
attempts to capture Ceuta, one of the keys of the strait separating
Africa from Europe, both of which the gallant behavior of the
governor, Count Julian, rendered ineffectual, Musa appointed Tarik-
Ibn-Zeyad, a Berber convert, formerly his slave, and now one of his
most trusty officers, to the command of Tangier, and returned to
Kairoan.
With the surrender of Tangier the Byzantine domination in Africa
came to an end. Sixty years of warfare, the destruction of fleets, the
annihilation of armies, the devastation of provinces, the enslavement
of nations, had been required to accomplish this result, never for a
moment lost sight of by the Moslems amidst the imbroglios of courts
and the revolts of pretenders to the Khalifate of Damascus. The
abnormally perfidious and martial character of the Berber placed him
outside the category of ordinary enemies. No reverses, however
severe, could break his spirit. He ignored the obligation of treaties.
No resource remained, therefore, but depopulation. The number of
slaves made by the Mussulmans in Africa excited the amazement of
their brethren in the East. A successful campaign often yielded two
hundred thousand of these unfortunates. Such wholesale captivity
was without precedent even in the annals of Rome. The fortresses,
with the exception of Ceuta, which was nominally a dependency of
the Visigothic kings of Spain—though held by a feeble and uncertain
tenure—were now in the possession of the Saracens.
The Berbers either paid tribute to the Khalif or, serving under their
own commanders, were enrolled in his armies. Already, after the
expiration of only two generations, during which the laws and
customs of Mohammedan life can be said to have been established,
the momentous effects of polygamy were strikingly noticeable. The
children of the pagan slaves who filled the harems of the conquerors
were educated in the doctrines of the Koran, and idolatry had totally
disappeared, save, perhaps, in some sequestered valley of the Atlas
Mountains, where the half-savage devotee bowed before a rude and
lonely altar, and with mystic incantations invoked the aid of some
misshapen image. Islam, which, even by the reluctant testimony of
Christian missionaries, exalts the character of the Negro and invests
him with a sense of personal dignity and self-respect which no other
religion has been able to inspire, soon gained the professed
allegiance of the Berbers; and like the Arab, the more suspicious and
clannish they had been in their Age of Ignorance, the more patriotic
and enterprising they became as Mohammedans—the very isolation
and irreconcilable antagonism of their former condition seemed to
insensibly impress them with a realization of the imperative necessity
and paramount value of national union. The call to prayer of the
muezzin everywhere rang out from the towers of pagan temple and
Christian church, whose magnificent decorations, bestowed by
penitent Goth and Vandal, had once glittered as trophies amidst the
splendid pageantry of a Roman triumph. But, despite community of
interest, ethnological resemblance, and identity of religious belief,
the environment of the inhabitants of Africa seems to be hostile to
the permanent improvement of the human species, and before
attaining to the highest degree of development of which the race is
elsewhere susceptible, it begins to retrograde. The natural state of
this great continent, determined largely by climatic and other
physical conditions, is essentially and eternally barbarous. Unlike
Europe, which has reaped something of value even from its
misfortunes, and, by the example of its achievements in art and
letters, subdued its very enemies, the institutions and influence of
no polished people have ever impressed upon the natives of Africa
any enduring traces. The astounding expansion of the Arab intellect
—the crowning phenomenon of the Middle Ages—was as transitory
in its effects upon them as the thrift and refinement of Carthage or
the more solid and majestic influence of Rome. In some respects
resembling Asia—whose voluptuous idleness tends inevitably to
physical and mental degeneracy—Africa, with its vast mineral
resources, its unsurpassed facilities for commercial intercourse, and
its inexhaustible agricultural wealth, has—with the exception of
Egypt, whose isolation rendered it practically a foreign country—
been of little use to its inhabitants, alike incapable of appreciating
these manifold advantages and of systematically employing them for
their own benefit or for the general profit of mankind.
CHAPTER IV
THE VISIGOTHIC MONARCHY

507–712
Origin and Character of the Goths—Their Invasion of the Peninsula—
Power of the Clergy—Ecclesiastical Councils—The Jews—The
Visigothic Code—Profound Wisdom of Its Enactments—Provisions
against Fraud and Injustice—Severe Penalties—Its Definition of
the Law—Condition of the Mechanical Arts—Architecture—
Byzantine Influence—Manufactures—Votive Crowns—Agriculture
—Literature—Medicine—Slave Labor—Imitation of Roman
Customs—Parallel between the Goths and the Arabs—Coincidence
of Sentiments and Habits—Causes of National Decline—
Permanent Influence of the Gothic Polity.
Among the countless hordes of barbarians who in the third and
fourth centuries overran the provinces of the Roman Empire, and
insulted the majesty of the sovereigns of the East, none were so
pre-eminently distinguished for valor, loyalty, generosity, and chastity
as the Goths. From the third century, when the luxurious tastes of
Rome impelled adventurous traders to penetrate to the shores of the
Baltic in search of amber, to the establishment of an independent
monarchy in Italy by Theodoric in the fifth, their name was familiar
to Europe—now suggestive of a bulwark of the tottering throne of
Byzantium, and again, as a synonym of murder, pillage, and
devastation. Of towering stature and fierce aspect, their forms were
cast in the gigantic proportions which pagan mythology loved to
attribute to its gods and heroes. Their habitations were situated in
the depths of gloomy forests, on the banks of deep and rapid
streams, or were surrounded by marshes, over whose treacherous
and yielding surface a winding pathway usually led to a remote and
well defended stronghold. Like all people whose intellectual
development had scarcely begun, they believed implicitly in omens,
auguries, signs, and dreams; their religious ideas were vague and ill
defined; and neither history nor tradition has preserved for us the
appellation or attributes of a single Gothic divinity. At their banquets,
defiled by drunken orgies, and not infrequently the scenes of
violence and even homicide, were celebrated, in uncouth ballads, the
exploits of the famous warriors of the nation. Their very name,
indicative of the superiority which their prowess never failed to
exact, signified The Nobly Born. Without literature, save a
fragmentary translation of the Bible, without government, save the
dominion of some chieftain who, covetous of renown, temporarily
enjoyed the precarious title of sovereign, eager for change, the most
reckless of gamesters, the most pitiless of conquerors, destruction
was with them a passion, and war an amusement. In common with
other barbarians, with whom the ignorance and fears of the age
have confounded them, they claimed and exercised, to the utmost,
the privileges of individual dignity and personal freedom. An
arbitrary classification, dependent upon a fortuitous geographical
distribution, had divided this people into Ostrogoths and Visigoths,
according to their relative location upon the eastern and western
banks of the river Borysthenes. The pressure from the north, which
had dispersed the tribes of the forests of Germany and Pannonia
over the European provinces of the Roman Empire, had induced the
Visigoths, by necessity or choice, to seek a home in Gaul, which
country they occupied in common with the Vandals, the Suevi, the
Alani, and other more obscure, but not less formidable, barbarians;
and scarcely had the division of the empire between Arcadius and
Honorius been effected in the first years of the fifth century, when
the inhabitants of Spain, either through treason or from the
negligence of the garrisons stationed in the passes of the Pyrenees,
were overwhelmed by a deluge of savage marauders.
For four hundred years, the beautiful, the rich, the fertile and
densely populated Peninsula had enjoyed the inestimable blessings
of peace. With the defeat and death of the sons of Pompey, the last
vestige of civil war and intestine discord had disappeared from its
borders. Its fields were cultivated with assiduous care; its seaports
were thronged with the shipping of the Mediterranean; the
manufacturing interests of its inland cities were diversified and
important. Its people, who had inherited from Rome and Carthage
that love of pleasure which was at once their boast and their
disgrace, with Epicurean unconcern, lived only for the present, in the
participation of all the luxury which boundless wealth and national
prosperity could bestow. Upon this earthly paradise—with its
splendid cities, its sumptuous villas, its majestic souvenirs of Roman
greatness, its traditions of heroic achievement and maritime
adventure; where Hannibal had gained his boyhood’s laurels, and
Cæsar, moved by the sight of Alexander’s statue, had first aspired to
the dominion of the world—now descended the brutal and licentious
plunderers of the North. The excesses perpetrated by them in other
provinces of the empire were trivial when compared with the havoc
they committed in Iberia. No considerations of public policy, no
sentiments of mercy, interposed to mitigate the calamities which
befell the smiling plains of the Anas, the Iberus, and the Bætis. Such
of the inhabitants as were fortunate enough to find an asylum
behind the walls of fortified cities, soon paid for their temporary
security with the pangs of famine. The growing crops, delivered to
the torch, left to-day a blackened waste where only yesterday had
been every promise of an abundant harvest. A smoky pall,
appropriate symbol of destruction, overhung the sites of prosperous
hamlets and marble villas, where a few smouldering embers alone
indicated the former abode of taste and opulence. Heaps of corpses,
denied the rites of sepulture, covered the land, which was infested
with incredible numbers of wolves and birds of prey, attracted from
every side to their loathsome and inexhaustible repast. A feeling of
utter despair fell upon the survivors; the instincts of humanity and
the feelings of nature were suspended or destroyed; men murdered
their families and then committed suicide; women devoured their
offspring; exposure, want, suffering, and anxiety produced their
inevitable consequences; and the crowning misfortune, the
pestilence, daily claimed its victims by thousands. The savage
masters of the country, satiated with rapine and mutually jealous of
power, now began to quarrel with each other. In the contests which
ensued, almost from the first, the superior organization and martial
genius of the Goths acquired for them the acknowledged supremacy
over their adversaries—a supremacy which soon became coextensive
with the Peninsula and laid the foundations of an extensive kingdom.
Early in the fifth century the extermination, expulsion, or absorption
by intermarriage, of the various tribes, and the emigration of the
Vandals, in a body, to Africa, gave the control of the entire country,
with the exception of a few seaports still tributary to Constantinople,
to the Visigoths. In political organization, in nomenclature, in the
construction and in the application of the maxims of jurisprudence,
in the election of their rulers, in the punishment of criminals, in the
regulation of their amusements, they observed the traditions and
honored the observances of their old homes on the Vistula and the
Baltic. The accident of conversion, a matter of indifference to the
majority of the nation, and one, in this instance, partially dependent
upon policy, had made them Arians, and consequently heretics. The
Gothic Church, in its independence of the See of Rome, while it
honored the Supreme Pontiff, and recognized, to a certain extent,
the religious supremacy of the Papacy, presented an anomaly in the
Christian world. The monarch chosen for his wisdom or his bravery
had not as yet assumed the exterior insignia of royalty, and the laws
held him to a strict accountability for the lives and property of his
subjects, but in ecclesiastical affairs his authority was undisputed
and supreme. He convoked at his pleasure and presided over the
national councils—assemblies originally composed entirely of the
clergy, and in which, at all times, the theocratical element largely
preponderated; he published encyclical letters; he possessed the
power of revising the decrees of councils before their adoption and
promulgation; and his wishes and suggestions were received with a
respect surpassing that usually accorded by his haughty vassals to
the majesty of the throne. The clergy were in fact absolutely
dependent upon the sovereign; their immunities were subject to his
will or his caprice; and, far from enjoying the exemption they
obtained in after times by reason of their sacred office and superior
sanctity, they were liable to taxation, and amenable to punishment
for the violation of the laws as strictly as were the laity. Not only
were these restrictions imposed upon them, but the interests of the
secular portion of the community were carefully guarded against the
possible encroachments of ecclesiastical tyranny; the judges were
particularly enjoined to scrutinize the conduct of the priesthood; and
instances were by no means rare where heavy fines were imposed
upon them for acts of injustice and for the oppression of their
parishioners. From the decision of every bishop and metropolitan an
appeal lay to the throne, a privilege conceded. to the meanest
peasant; the king could suspend or abrogate the rules of
ecclesiastical discipline; no canon was valid without his sanction; and
he assumed the rights of nominating, and of translating from one
see to another, the greatest prelates of the Church. But as
assemblies of men who possess a monopoly of the learning and
worldly wisdom of a nation, conscious of mental superiority and
incited by motives of ambition, are never satisfied with acting in a
subordinate capacity; the ecclesiastical councils of Spain almost
imperceptibly, but none the less surely, began to encroach upon the
royal prerogative, and, assisted by the weakness or gratitude of
princes whose titles had been assured by their confirmation, aimed
at the seizure of absolute power. By the institution of the rite of
anointing, which imparted a sacred character to the monarch, and
invested in them an implied control over his coronation—a rite first
used in Spain and not adopted in France till the reign of Pepin, in the
eighth century; by the framing of laws favorable to their order, and
whose essential provisions were carefully disguised under the
specious name of enactments for the public welfare; by a command
of a majority of the votes which elected the sovereign; and lastly, by
the conversion of the whole nation to the doctrines of the orthodox
faith; the Gothic clergy advanced unswervingly towards the
establishment of their claim to political supremacy. The Third Council
of Toledo was the first of these important convocations in which
questions relating to the settlement of the constitution of the Gothic
monarchy were debated and settled. From this time until the
meeting of the Eighth Council in 653, the palatines did not
participate in the deliberations of these assemblies, which now
began to assume the appearance of legislative bodies, in which the
aims of exclusive ecclesiastical representation were already clearly
disclosed by the partiality and exemptions which characterized the
canons treating of the rights and privileges of the priesthood. After
the middle of the seventh century, although the nobles were
admitted as members of the national councils and took part in their
discussions, the influence of the clergy became paramount, and the
duties of the nobility were confined to a passive assent to, and
registration of, their edicts. A separate tribunal for the final
adjudication of all disputed points of doctrine which might
incidentally arise in the ordinary administration of justice was
granted to ecclesiastics; the latter were prohibited from engaging in
commerce, which the poverty of the Church had formerly rendered
necessary; it became customary to select bishops for the negotiation
of treaties, and for the direction of military embassies which were
invested with the all-important powers of peace and war; the
councils occasionally claimed jurisdiction over secular causes—an
unwarranted assumption of power which the indifference or bigotry
of the sovereign usually failed to resent; and the intolerant character
of the canons treating of heresy indicate, but too plainly, the
growing spirit of persecution—the germ of future inquisitorial
atrocities.
But, notwithstanding the acceptance of Catholicism, and the
consequent advance towards the enjoyment of absolute
independence, the Church was hampered by many serious
restrictions. Bishops, clerks, and monks remained subordinate to the
secular arm and responsible to the courts of the realm; they could
not, with impunity, disregard their processes, still less defy their
authority; and the commission of crime rendered them liable to
heavy fines and long terms of imprisonment; although, like the
nobility, they could not be subjected to the punishments inflicted
upon the lower orders, such as scourging and branding—the latter
being considered especially infamous. The immunity which
subsequently attached to the character of the clergy as non-
combatants was not known to the founders of the Gothic monarchy.
When a city was besieged or the country threatened with invasion,
every subject, regardless of his profession, was obliged to serve in
the army, and no ecclesiastic could plead his sacred office in bar of
military duty to his sovereign, under penalty of confiscation and
exile; the tonsure was regarded as of peculiar significance and
sanctity, and any one whose locks had once been shorn, or who had
assumed the clerical habit, was henceforth excluded, as a rule, from
all military and civil employments, and consecrated for life to the
service of the cloister; a law which, when abused by fraud or
ignorance, was more than once productive of important results, and
even of changes in the royal succession. Upon the whole, however,
the influence of the Church in those days of intellectual darkness
was highly beneficial. Its monopoly of the scanty wisdom of the time
was often employed for the protection of the oppressed, for the
alleviation of suffering, for the frustration of tyranny, for the
consolation of death. The bishop stood as a guard between the
helpless peasant and the unjust judge; his mediation with the
throne, in cases of flagrant injury, was not optional but mandatory;
and his official conduct was subject to the constant supervision, and
was liable to the censure, of the magistrate. The ambition and
political aspirations of the clergy, joined to their insatiable greed of
dominion, which increased with each successive encroachment upon
the civil power, with the daily accumulation of wealth, and the
acquisition of extensive estates by gift, extortion, bequest, or
purchase, disclosed themselves in time in their legitimate
consequence, religious intolerance. The Arian Church in Spain never
disgraced its rule by persecution for differences of opinion. With the
acceptance of the orthodox belief in the sixth century, however, the
spirit of vindictive malevolence,—which has always animated and
directed the genius of Catholicism when in the ascendant, at once
infected the counsels of the ecclesiastical tribunals, and indirectly,
through their influence and example, the decisions of the courts of
law. The coronation oath rendered obligatory the expulsion of all
heretics without consideration of birth, position, or previous service
to the state. The Jews, in whom were vested the most important
offices, and who possessed the bulk of the wealth of the kingdom,
were banished, imprisoned, plundered, or burnt; and while it is true
that the severity of the laws against this sect defeated, erelong, the
object of their enactment, even their partial enforcement was the
cause of great and wide-spread suffering. With the consciousness of
power came the increase of pomp and the desire for prohibited
enjoyments and indulgence in carnal pleasures wholly inconsistent
with the observance of the vows of poverty and chastity as well as
contrary to the rules of ecclesiastical discipline. The canons enacted
from time to time by the councils, and whose provisions were
designed to impose restraints upon the irregular conduct of the
clergy, show, more conclusively than the pages of any chronicle, the
lax morality and deplorable condition of the religious society of that
age. Stringent regulations were adopted against the acceptance of
bribes as the price of exemption from persecution—especially
referring to the Jews—a proof that the zealous protestations of the
clerical order could not withstand the pecuniary arguments of the
astute Hebrew; while the censures fulminated against priests and
monks who abused the privileges of the confessional, or violated
nature in the commission of revolting crimes, indicate the secret and
universal corruption which had already begun to pollute the sacred
offices of the Church and impair the usefulness of its ministers. The
Eighteenth Council of Toledo, at the dictation of King Witiza, whose
profligate conduct and contempt for religion had aroused the horror
of Christendom and provoked the anathemas of the Pope, had, with
unexampled servility, passed laws authorizing the marriage of
ecclesiastics, the institution of polygamy, and the practice of
promiscuous concubinage. Under these conditions of sacerdotal
degradation, sanctioned by custom and established by law, the
influence of the Church was everywhere diminished; the faith of men
in the existing religion was weakened; and the public mind was
insensibly prepared for the new revelation which, appealing to the
strongest passions of the human breast, stripped of metaphysical
distinctions, and inculcating moral precepts such as the most
skeptical and dissolute must applaud, was soon to be published to
the discontented and priest-ridden subjects of the Gothic empire.
The ill-defined powers of the Crown and the Mitre, at first
reciprocally dependent, led eventually to a clashing of interests and
a struggle for precedence between the royal and the sacerdotal
authority, in which the clergy, though their aspirations were
occasionally checked by some monarch of stern and decided
character, in the end invariably obtained the advantage. The
dependence of the sovereign upon the priesthood was never lost
sight of. No occasion which might remind him of the obligation he
owed to the order whose suffrages had conferred, and might, with
equal facility, resume possession of his crown, was suffered to pass
unimproved. The anointing with holy oil, which symbolized the right
of divine consecration, had already forged another link in the chain
which bound the king to the Church. The anathemas denounced
upon a prince for failure to execute the laws against heretics, far
exceeded in virulence those to which any subject was liable. At one
time, the wishes of the sovereign were anticipated by the
subserviency of the prelates; at another, his prerogative was invaded
and his commands disobeyed with an arrogance worthy of the
imperious spirit of Julius II. or of Gregory the Great. The populace,
through ignorance, prejudice, and habit, blindly devoted to the
sacerdotal order, furnished a formidable body of auxiliaries, ever
ready to hearken to the appeals of their ghostly advisers, a force
which the dignity and assurance of the haughtiest ruler could not
with impunity disregard. The turbulent and illiterate nobility,
although the king was selected from their number by the voices of
the assembled bishops—in which ceremony the concurrence of the
palatines was admitted, in reality, only through courtesy—possessed,
in the practical application of the precepts of the Gothic constitution,
scarcely the shadow, still less the substance, of power. The council
was the embodiment and representative of the intellect and the
collective wisdom of the nation. Its canons were, for the most part,
framed in strict accordance with the principles of equity, and the
deliberations and conclusions of its sessions were often
characterized by a breadth of understanding and a degree of
impartiality which clearly indicated that its members were not
deficient in the knowledge and requirements of enlightened
statesmanship. The results of their labors are contained in the Gothic
Code, a body of laws remarkable in many respects, when we
consider the general illiteracy and ignorance of the age in which it
was compiled, and its transcendent importance as the prototype of
the systems of jurisprudence which now regulate the civil and
criminal procedure of the courts of Europe and America. In the
extraordinary minuteness of its details, in its thorough and
comprehensive treatment of the manifold transactions of daily life,
and in its provisions for almost every contingency which could arise
in the administration of the sovereigns under whose auspices it was
framed, this extraordinary work presents the modern legislator with
a subject eminently worthy of his attention and study. The contact
with races which had long enjoyed the blessings of civilization, and
the development of the intellectual faculties consequent upon the
experience obtained in frequent expeditions and protracted
campaigns, imperceptibly modified the ancient laws of the Goths;
the very essence of which was, from the first, and long continued to
be, the assertion of the principle of personal liberty. Rome, whose
toleration of the religious prejudices and customs of the nations
subjected to her dominion—so long as they did not conflict with her
interests or contravene her authority—was one great secret of her
power, had, in accordance with that policy, indulged the Iberians in
the use of their own laws, and only those who enjoyed the privileges
of citizenship could be summoned before the tribunal of the imperial
magistrate. The incursions of the barbarians had abolished every
restraint, and transformed the previous quiet and peaceful condition
of the Peninsula into a state of anarchy. There was then no law but
the will of the chieftain, who was inclined to encourage, rather than
to repress, the excesses of a brutalized soldiery. All records and
muniments of title had disappeared; boundaries had ceased to exist;
the tenure of lands was entirely dependent upon the numerical
strength of the claimants; and when the fields of one district were
exhausted, the discontented settlers sought a new residence in
another locality, whose wealth had excited their avarice, and the
inferior military resources of whose occupants rendered the retention
of their possessions uncertain. The cessation of hostilities was
always accompanied with the plunder and impoverishment of the
vanquished; no treaty was valid, because no moral obligation, or
superior power by which it could be enforced, existed; every vice
was committed with impunity; every grudge was satisfied with all
the abuse of unrestricted license; the caprice of the military
commander had supplanted the precedents of the prætor, and the
sword had become the only acknowledged arbiter of every
controversy.
During the reign of Euric, in the year 479, was codified and
published the first book of Gothic law, the basis of the subsequent
complex and exhaustive system of jurisprudence which increased in
size, and gathered reverence and authority with the reign of each
succeeding sovereign. It was known as the Forum Judicum, or the
Book of Judges, and consisted mainly of a compilation of the rules
applicable to the various customs and ordeals, which had been
approved by time and experience as beneficial in the administration
of the government of the Gothic nation, combined with such maxims
of Roman law as had gradually been absorbed through frequent
association with the courts and magistrates of the empire. The new
rights and duties arising from the acceptance by the Goths of the
orthodox belief in the latter half of the sixth century, necessitated a
revision of the existing laws and the formulation of another code of
far more extensive scope than the one which already existed. By
certain provisions of the former the constitution of the Iberian
church was definitely established and the predominance of the
clergy in secular matters assured; measures of portentous
significance, whose evil effects upon the intelligence and prosperity
of the Spanish people are discernible even in our day. From the date
of its adoption and promulgation, the inhabitants of the Peninsula
were, without exception, declared subject to its statutes. From this
time dates the absolute supremacy of the Church in the Peninsula.
The hold which it then obtained upon temporal affairs it has never
relaxed. The awful consequences of that supremacy upon all classes
and conditions of men owing allegiance to the Spanish crown are
familiar to every reader of history.
The Visigothic Code exhibited, in the restrictions it imposed upon
the royal prerogative, that spirit of jealous independence always
conspicuous in the character of the German warrior, and which had
been preserved through many centuries by the importance that
distinguished the privileged orders under an elective monarchy. The
king, who, at first, had been liable to censure and judgment by his
subjects, was informed, when invested with his office, that even its
dignity could not exempt him from the obligation to observe the law,
a principle of justice and equality which he shared with every
resident in his dominions. The authority of the turbulent and illiterate
nobles, who, with all the arrogance of power, did not hesitate to
threaten and insult the creature of their choice, was curbed in time
by the potent yet gentle influence of the clergy, whose learning and
talents at first swayed, and finally absolutely controlled, the
deliberations of the National Councils. The high rank of the prelates,
their superior accomplishments in an age of universal ignorance, and
their claims as members of an independent hierarchy, which even
the Supreme Pontiff himself scarcely ventured to contradict, in the
end communicated to the Visigothic constitution all the worst
characteristics of an irresponsible and intolerant theocracy.
The Forum Judicum consists of twelve books, which not only
define the rights of the different classes of society, but prescribe at
length, and in copious detail, the mode of procedure to be followed
in the various tribunals. Every precaution which ingenuity could
devise was adopted to insure the fidelity, the honesty, and the
impartiality of the magistrate, whether of the civil or the
ecclesiastical order. It was the duty of the judge to observe and
report upon the decisions of the bishop and the priest, while, on the
other hand, the higher clergy possessed, under certain
contingencies, the power of examining causes and rendering
judgment when the proper official had refused or neglected to
exercise his judicial functions, and the interests of either of the
parties litigant were exposed to injury in consequence. The courts
were open from dawn to dark, and the period of vacation and the
hours of rest were strictly regulated by law. The trial of causes could
not be delayed except for valid reasons; the speedy rendition of
judgment was compulsory; the procrastination, injustice, or
corruption of the judge was punished by a fine amounting to double
the loss incurred, and when the circumstances were peculiarly
aggravating his property was confiscated and he was publicly sold as
a slave. No person, however indigent, was debarred, for that reason,
from the benefits of justice, and a fund was set apart in every town
for the support of impecunious litigants, which was disbursed by the
municipal government with the approval of the bishop. An appeal
from the decisions of the inferior tribunals was granted as a matter
of unquestionable right, and the slightest suspicion of interference
by the throne in the proceedings rendered them invalid and
worthless. The ceremonies relating to the administration of the law
were characterized by great simplicity, and the pleadings were
divested of unnecessary verbiage. The highest reverence for the
officers of the crown was inculcated and enforced; and a resort to
litigation was persistently discouraged by public opinion, excepting
where it was imperatively demanded by the interests of justice. In
the rules of evidence, as well as in their application, traces of the
deeply rooted superstitions of the Teutonic barbarians still remained.
The ordeals of fire and water were not infrequently adopted. The
wager of battle could not be refused, without ignominy; and the
oaths of compurgators were, at times, invoked to restore the lustre
of some tarnished escutcheon, or to remove the stain attaching to a
suspected violation of female honor. Torture was allowed, but
excessive severity in its application was prohibited, and, in case of
death or permanent injury resulting from its abuse, the judge was
liable to forfeiture both of his possessions and his liberty. In
determining the competency of testimony, an unwise and unjust
discrimination was made against the poor, through the
unwarrantable presumption of temptation to bribery, and this
exclusion also applied to Jews—even though apostates—as well as
to their descendants, and to slaves. The crime of perjury was
mentioned with horror; its commission was deemed worthy of the
severest punishment; and the false witness, visited with public
execration, was condemned to life-long servitude. In general, the
criminal code of the Visigoths was conspicuous for the moderation
with which it treated offenders against the public peace. The penalty
of death was rarely inflicted, and was confined to cases of arson,
rape, and murder. A regular schedule of minor crimes and their
punishments existed; the severity of the latter depending upon the
social rank and political importance of the individual. In flagrant
instances of malicious prosecution, bribery of public officers, or
abuse of political power, the culprit became the slave of the injured
party, with the sole limitation to his resentment, that the life of his
former oppressor should be spared. Rebellion was punished by
banishment; infanticide by blinding; and the counterfeiter, or the
forger of a royal edict, suffered the loss of the right hand. When the
atrocious nature of an offence against morals demanded a penalty of
corresponding infamy, the head of the criminal was shaved and
branded, marking him for life as a social outcast, to be forever an
object of public abhorrence. Scourging was the penalty of most
universal application, and even a freeman, however exalted his
station, was not exempt from its infliction, if he ventured to provoke
the vengeance of retributive justice, and was not possessed of the
stated fine which was the legal equivalent of the lash. The right of
asylum, a privilege whose importance as a salutary check upon the
passions of a fierce and tyrannical nobility, in an age of violence, is
with difficulty appreciated in modern times, was recognized by the
Gothic constitution; and no suppliant, who had sought protection at
the foot of the altar, could be removed without the consent of the
proper ecclesiastical authority. In the provisions which define the civil
relations of society, the Forum Judicum recalls to every one
conversant with the Commentaries of Blackstone, the familiar
maxims and precedents of the Common Law of England. The
different grades of relationship, and the rights of inheritance in the
ascending and descending lines, were treated of exhaustively in the
books of the Visigothic Code. In the protection of the interests of
children its sections displayed a paternal and anxious care. No child
could be disinherited unless it had been guilty of some aggravated
act of violence towards its parent. In all questions relating to the
descent of property, no preference was accorded to sex, and the
female remained on the same footing as the male. A minor of ten
years could, without restriction, dispose of his or her possessions by
will. Guardians were appointed by the courts, who were required to
observe the conditions of their trust, and to render accounts of the
funds which passed through their hands; and the power of
appointing a guardian ad litem was frequently exercised, where the
affairs of a minor necessitated the institution or the defence of a suit
at law. The boundless control of the father over the child, which
formed so prominent a feature in the domestic regulations of Rome,
was repugnant to the independent spirit of the Goths; the parental
duties and responsibilities were expressly defined; the son who
resided with his father was entitled to two-thirds of his earnings; and
the courts exercised unremitting and vigilant supervision over the
persons and estates of minors and orphans. A reminiscence of the
ancient custom of marriage by purchase survived in the price paid by
the bridegroom to the relatives of the bride; all clandestine alliances
were considered invalid; a woman could sue, and be sued, without
joining with her husband; and no responsibility attached to either for
the illegal acts of the other. Integrity of descent and purity of blood
were preserved by laws of exceptional severity; a free-born female
who abandoned her person to, or even contracted marriage with, a
slave was scourged and burnt with her unfortunate paramour or
spouse. A wife who had incurred the guilt of adultery was delivered
over absolutely to the tender mercies of the injured husband. This
offence, which evoked ordinarily the strongest denunciation from the
descendants of the cold and sluggish barbarians of the Baltic, was,
however, in an ecclesiastic rather reprobated as an amiable
weakness than condemned as a crime; an indulgence to be
attributed partly to the predominant and sympathetic caste of the
legislature, and partly to an appreciation of the opportunities and
temptations which beset the father-confessor, who, after conviction,
was immured in some comfortable monastery until he professed
penitence and received absolution.
The conditions of vassalage and serfdom, as understood and
practised elsewhere in Europe, and especially in Germany, were
foreign to the polity of the Visigoths. Feudalism, with its mutual
rights and obligations as subsequently known to Europe, strictly
speaking, did not exist. The relations affecting the status of lord and
vassal were, to some extent, borrowed from the Roman system and
modelled upon those of patron and client. The sections relating to
the conditions of servitude were minute and voluminous. The master
had generally unrestricted power over the life of his slave. He who
aided the escape of the latter was legally responsible for his value.
Recognizing the peculiar facilities for criminal intercourse, and the
corresponding difficulty of its detection, the law sentenced the
servile adulterer to the stake. While the most liberal encouragement
was given to the manumission of slaves, the numbers of this
unfortunate class were constantly increasing, by the capture of
prisoners of war, by the degradation of dishonest officials, by the
submission of debtors, and by the conviction of criminals. Every
slave belonged to a certain rank, and castigation for petty
delinquencies, as well as punishment for serious crimes, was inflicted
with more or less rigor, according to the cause of his servitude, his
industrial ability, and the social condition of his owner, whether he
was born, purchased, or condemned; whether he was a skilful
artisan or mechanic, or an ordinary laborer; or whether he was the
property of the Crown, of the Church, or of an individual. The
influence of the Visigoths did much to lighten the burdens of slavery;
the bloody spectacles of the gladiatorial contests possessed no
allurements for a nation not degraded by cowardice and cruelty; the
treatment of bondmen was, in some localities, so softened and
modified that scarcely more than the name of hereditary servitude
existed; and in cases of intolerable oppression, where the slave took
refuge in the sanctuary, the master could be compelled to dispose of
him to some one more actuated by feelings of kindness and pity.
The precepts of the Forum Judicum which relate to bailments, to
strays, to trespass, to accessories before and after the fact, to the
obstruction of highways, to malicious mischief, to the attestation of
documents, and to contracts made under duress, are substantially
the same as those set forth in our law-books of to-day. A statute of
limitations, which recognized a period varying from thirty to fifty
years, beyond which even some criminal prosecutions could not be
instituted, was in force. The legislation pertaining to agriculture,
irrigation, and the boundaries of land was particularly complete and
exhaustive. Security was obtained by bonds and pledges; inventories
were required of guardians; and the culprit who was guilty of
slander was not only responsible in damages for his intemperate
language, but was also often liable to corporeal punishment; as, for
instance, if he called another a “Saracen,” or even insinuated that he
had been circumcised, he might consider himself fortunate if he did
not receive fifty lashes at the hands of the common executioner.
Considering the general condition of society, the antecedents of a
nation whose energies had hitherto been directed to the overthrow
of every institution which secured the perpetuity of peace and order,
the previous slender opportunities of its authors, and the limited
educational facilities at their command, the Code of the Visigoths
presents us with a system of legislation of extraordinary interest and
value. So remarkable is this body of jurisprudence in the wisdom,
foresight, humanity, and knowledge of mankind which characterize
its leading maxims, that they almost seem to have been suggested
by divine inspiration. Its first statutes appeared when the
comprehensive system of Justinian, which had enlisted the talents
and exhausted the erudition of the most accomplished jurists of the
Eastern Empire, was nearly perfected. It borrowed but little,
however, from the learning of Tribonian and the laborious ingenuity
of his seventeen coadjutors. The eternal principles of justice, it is
true, are equally the basis of both of these collections; but their
construction and the methods of their application, under similar
conditions, are widely different; and the superiority, upon the whole,
is largely on the side of the so-called barbarian. In the majority of
instances, excepting where ecclesiastical ambition and monastic
prejudice perverted the ends of legislation, the laws of the Visigoths
were uniformly framed for the protection of the weak, the relief of
the oppressed, and the general welfare of society. Unlike the practice
of more civilized nations in comparatively recent times, the
judicature of the former confined its penalties to the personality of
the offender, and imposed no disabilities, either by forfeiture or
attainder, upon his innocent relatives and descendants. It restrained
the tyranny of the monarch; it defined with conciseness and
accuracy the rights of the subject; it accorded unprecedented
concessions to the widow and the orphan; it respected the
unfortunate and helpless condition of the slave. It prohibited
encroachments upon personal liberty, and declared the sale of a
freeman to be equivalent in atrocity to the crime of homicide. In
almost every provision which did not conflict with the claims of the
priesthood, it hearkened to the voice of mercy and humanity. By the
constant menace and certain infliction of civil degradation,
confiscation, and perpetual servitude, it secured the fidelity of the
judges and fiscal officers of the state. It accepted the great principle
of the Salic law, and, with worldly prudence, forbade the election of
a female sovereign. But, when the theocratic influence which
pervaded every branch of the Gothic constitution comes to be
examined, its effect upon contemporaneous legislation is seen to be
pernicious and deplorable. The power of the clergy was
irresponsible, ubiquitous, and thoroughly despotic. It dictated the
proceedings of every assembly. It whispered suggestions of
questionable morality in the ears of the monarch. When thwarted in
its unholy aims, its vengeance was implacable. The abuse of the
convenient and formidable weapon of excommunication had not
reached the extreme which it subsequently attained, yet the all but
omnipotent hand of the priesthood was already able to invade the
privacy of domestic life, to interfere with the sensitive and delicate
mechanism of commerce, to violate the rights of property, to
desecrate the sacred precincts of the grave. Ecclesiastical intolerance
dictated the passage of ex-post-facto laws, a measure whose
monstrous injustice is patent to every unprejudiced mind. The
disability imposed upon the Hebrew race, and the savage spirit of
the canons enacted for its oppression, point significantly to the
prospective horrors of the inquisitorial tribunals. The practice of
sorcery and magic—so dreaded in an age of intellectual inferiority,
and especially offensive to the Church, which tolerated no wonder-
workers outside of its own pale—was severely reprobated, and
punished with excessive severity. The ends of the clergy, when not
obtainable by the arts of controversy, were secured by other means
not unfamiliar to the intriguing courtiers of mediæval Europe; its
propositions were advanced with caution and debated with
consummate skill; and its arguments were either insinuated with
more than Jesuitical adroitness, or urged with all the energy of
sacerdotal zeal.
In its respectable antiquity; in the sublime morality inculcated by
its precepts; in the obligations incurred by every nation which has
drawn upon its accumulated stores of wisdom; in its freedom from
the dishonorable expedients of legal chicanery; in the simplicity of its
procedure; in the certainty and celerity required by the practice of
the tribunals where its authority was acknowledged; in the inflexible
impartiality with which it invested the decisions of those tribunals; in
its well-founded title to public confidence; the Visigothic Code is
without parallel in the annals of jurisprudence. But great as are its
claims upon the gratitude and reverence of the jurist and the
legislator, they are scarcely comparable to the indebtedness imposed
upon the historian. The meagre information to be gleaned from the
works of native chroniclers is, in great measure, thoroughly
unreliable. The literature of the age, scanty in itself, consists mainly
of the recital of ecclesiastical fables, the martyrdom of legendary
saints, the discovery of spurious relics, the averting of calamities by
invocation and miracle, and trivial incidents in the lives of holy men
and women, whose preternatural gifts the indulgent credulity of their
biographers has handed down to the contempt and ridicule of
posterity. The pages destined for such records were too precious to
be defiled by the accounts of wars and insurrections and the
interesting descriptions of mediæval society. The diligence of the
compilers of the Forum Judicum has, however, largely supplied the
deficiencies of the monkish annalists. In their various civil and
prohibitory enactments, they have unconsciously delineated the
follies, the vices, the superstitions, and the crimes of the age. The
penalties imposed for the violation of statutes denote infallibly the
barbarian origin of those who formulated them. The law of
retaliation—tolerated only among the lowest races of men—occurs
repeatedly among the provisions of the Visigothic Code. The
deterrent effect of criminal legislation was almost always
subordinated to considerations of vengeance. The magistrate was
regarded as the vindicator of wrong, rather than the calm
representative of judicial dignity and the impartial interpreter of the
laws. Scalping, maiming, blinding, scourging, branding,
emasculation, were punishments prescribed without discrimination,
for offences varying widely in the nature and degree of misconduct
and criminality. The period of transition which separated the barbaric
rudeness of Adolphus and the effeminate luxury of Roderick is
traceable, step by step, in the progressive legislation of centuries.
The rise and consolidation of ecclesiastical power; the limitation of
the royal prerogative; the decline of the insolent pretensions of the
nobility; the elevation of the peasant from the position of a beast of
burden to a self-respecting being, who, however steeped in
ignorance he might be, was always sure of an impartial hearing
before the magistrate; are there related with all the fidelity and
minuteness of a chronicle. There too are depicted the sources of
that inspiration which animated and sustained the sinking hopes of
the founders of the Spanish monarchy, from its organization as a
little principality in the Asturias, down through the turbulent era of
Moorish domination, until it attained the summit of greatness as the
dictator of Europe and the arbiter of Christendom. These are the
general characteristics of that incomparable monument of
jurisprudence whose noble conceptions of the ends of legislation are
best expressed in its own concise and energetic language:
“The law is the rival of divinity, the messenger of justice, and the
guide of life. It dominates all classes of the state, and all ages of
humanity, male and female, the young and the old, the wise and the
ignorant, the noble and the peasant. It is not designed for the
promotion of private aims, but to shelter and protect the general
interests of all. It must adjust itself to time and place, according to
the condition of affairs and the customs of the realm, and confine
itself to exact and equitable rules so as not to lay snares for any
citizen.”
Lost in the confusion attending the Conquest, the Forum Judicum
was carefully preserved by the Moors for the benefit of future
generations; and, recovered when the Moslem capital was taken by
St. Ferdinand, it was subsequently translated into Castilian.
Among the nations composing the heterogeneous population of
Spain, the most important in intelligence, wealth, commercial
activity, and talent for administration, in ancient times, were the
Jews. Classed with the first colonists of the Peninsula, the earliest
mention of Iberia by the Greek and Roman historians represents the
Jewish population as already rich and prosperous. If we consider
their intimate relations, kindred interests, alliances by marriage, and
common inclination for traffic, with their Tyrian neighbors, it is not
improbable that the settlement of the Hebrew in Bætica was
coincident with that of the Phœnicians. The first National Council
that assembled at Illiberis in 325—the same year in which were
determined the principles of orthodox Christianity as set forth in the
Nicene creed—inaugurated the long and bloody persecution which
finally culminated in the wholesale expulsion of the unfortunate race
by Philip V. By the canons of this council, the blessings of the rabbi,
to which the husbandman seemed to attach a virtue and an
importance equal if not superior to those presumed to attend the
benediction of the priest, and which custom from time immemorial
had invoked upon the growing crops, was declared an offence
against religion, punishable by summary expulsion from the Church.
The morose spirit of ecclesiastical bigotry did not hesitate to violate
the rites of hospitality and cast a shadow over the amenities of social
life. With an exquisite refinement of malice, it pronounced subject to
excommunication all who, even in cases of charity or under
circumstances of the most urgent necessity, shared their food with a
Jew. The passive submission of the entire race to the barbarian
invader procured, however, for its members, in many instances, a
degree of consideration not enjoyed by their Christian neighbors.
With their natural talents for business, their capacity for intrigue,
and, above all, their superior knowledge of mankind, they were not
long in securing the confidence of the conquerors. Under the Arian
sovereigns, their religious opinions remained for generations
unquestioned, and their worship unmolested. But hardly had the
nation renounced its ancient communion, before the disturbing spirit
of the new hierarchy began to assert itself. The edict of Sisebut, in
612, published the decrees of the Third Council of Toledo which had
been drawn up for the pious purpose of “eradicating the perfidy of
the Jews,” whose general prosperity and political power had aroused
the apprehensions of the priesthood. From this era until the
accession of Roderick in 709, the legislation of the councils relating
to the Jews presents the extremes of brutal harshness and
occasional liberal indulgence. In all these enactments, however, the
offensive qualities of injustice and malevolence largely
preponderated. The aggressiveness of Catholicism demanded instant
and uncompromising submission to its creed. What was at first
attempted by the imposition of civil disabilities was soon after
exacted by degrading insults, by torture, by slavery, and by death.
Such was the unrelenting ferocity of this persecution that it
awakened at times the indignation even of a semi-barbarous and
fanatical age. But despite continuous and systematic repression, this
maligned and down-trodden race prospered; the forbearance of
royal and ecclesiastical inquisitors was purchased, and the clamors
of furious zealots were silenced by opportune contributions to the
monastic orders; for the services of the most capable diplomatists
and financiers of the time could not be dispensed with in a society
where even a large portion of those who devised measures for their
oppression could neither read nor write. The superiority of the Jews
was also indicated by the prices they commanded when their liberty
had been forfeited by law. While slaves of other nationalities ranked
as “bestias de cuatro pies,” and were purchasable upon the same
terms as a horse or an ox, the Jew was worth a thousand crowns.
The great possessions of the Gothic nobles, which the universal
illiteracy of the latter made them incompetent to manage, rendered
the shrewd and accomplished Hebrew a necessary steward. He
enjoyed the confidence of the monarch. He administered the royal
revenues, always with discernment and in most instances with
fidelity. His advice was eagerly solicited in exigencies of national
importance, and in the crooked arts of diplomacy he proved more
than a match for the ablest negotiators of the age. His wealth, his
political and social influence, which he preserved in defiance of civil
disabilities and ecclesiastical malice, his scholastic attainments, the
elegance of his manners when contrasted with Teutonic rudeness; all
of these qualities ingratiated him into the favor of the palatines, by
whom he was often treated with the consideration deserved by a
friend, rather than with the abhorrence due to an outcast.
The political organization and legal privileges which the Jews
possessed in the early days of the Visigothic monarchy magnified
their importance, increased their wealth, and fostered their spirit of
exclusiveness. The latter feeling was also strengthened by the policy
of separation which it was deemed expedient to adopt, during the
Middle Ages, in Christian communities, towards the Hebrew race. For
a considerable period of the Gothic dominion, the Jews were
confined to a certain quarter of every city and village, over which
magistrates of their own blood exercised both civil and criminal
functions, unrestricted, save in questions that affected the national
faith or where personal injury had been inflicted upon a Christian.
The jurisdiction of each provincial assembly was rigidly subordinated
to the supreme authority of the central synagogue. The territory
beyond the limits of the town—which was often entirely Jewish—was
subject to the control of a governor who was responsible only to the
sovereign. At one time the Jews controlled the most important
landed interests of the kingdom. The prejudice attaching to
payments for the use of money did not deter the Hebrew banker
from the practice of usury, although the legal rate of thirty-three per
cent. certainly offered sufficient inducements to abstain from the
violation of the law which he either secretly evaded or openly defied.
The activity displayed by the Jews of the Peninsula in every
department of science, literature, government, commerce,
agriculture, and finance was incessant and indefatigable. No
contemporaneous people could boast, in proportion to their
numbers, so many men of genius and erudition. Their influence was
so extensive that it was acknowledged alike in the hovel of the
peasant and in the council chamber of the king. Their powerful
individuality survived the cruel impositions which repressed their
enterprise, but could not damp their ardor; and the patriotism which
attached them to a country in which they were only tolerated as
exiles, was sufficient to induce their descendants to heartily aid, by
every means in their power, the famous princes and warriors whose
capacity and resolution supported, amidst continuous disaster and
defeat, the doubtful fortunes of the struggling monarchy of Castile.
In their application to the mechanical arts, and in their
development of architecture, the Visigoths disclosed rather an
imitative faculty than a spirit of marked originality. What is known to
us as the Gothic style owes nothing to that nation to which popular
belief has ascribed its invention, and, in fact, was not introduced into
Spain until the thirteenth century. The name has been arbitrarily
given it to distinguish the pointed arch—its principal characteristic—
from the rounded one peculiar to the edifices of Rome. The rude and
primitive structures of the German forests, constructed of logs,
stained with mud, and designed solely for purposes of shelter and
defence, could neither suggest nor transmit traditions of
architectural elegance and beauty. The sight of the noble memorials
of Roman genius which had escaped the destructive impulses of the
predatory barbarian, erelong inspired the uncouth conqueror with
the spirit of emulation. In the Iberian Peninsula these vast and
splendid structures abounded. The walls which once encompassed
the seats of its proconsul; the fanes from whence had arisen the
incense to its gods; the colonnades which adorned its capitals; the
aqueducts rising to prodigious heights, and surmounting difficulties
which would have perplexed any engineer save a Roman, were
worthy of one of the richest provinces of the empire. From such
models the Visigothic architect, wholly destitute of experience, yet
animated by the desire of imitating an excellence which had
awakened his admiration, designed the palace and the basilica. The
wealth which, from the earliest times, Spain has lavished upon her
children, furnished the means, while the religious spirit which
pervaded every class of society afforded the incentive, for public
display and private munificence. An innumerable body of slaves and
dependents, available at a moment’s notice, facilitated the rapid
construction of edifices of the largest proportions. Churches grand in
dimensions and barbaric in decoration were erected by priests,
abbots, and private individuals, whose generosity was
commensurate with their devotion. Before the shrines of these
temples were deposited vases, reliquaries, diptychs, crosses, of
precious materials and curiously intricate patterns. The religious
enthusiasm of the Gothic princes, mingled perhaps with a certain
share of worldly ambition, impelled them to a generous rivalry, and
nourished in the bosom of each the desire to surpass his
predecessor in liberality to the Church. Hence the various temples
were, under each successive reign, enriched with royal gifts of
inestimable value and ostentatious magnificence. Sacramental tables
of gold studded with emeralds, diamonds, and sapphires, whose
wondrous beauty and richness Saracen tradition has transmitted to
posterity, with monstrances and ciboria of ingenious design and
encrusted with jewels, formed a portion of the pious donations of
the sovereigns of the Goths. The influence of the arts and taste of
Byzantium, communicated through the channels of commerce, the
interchange of civilities, and the frequent intercourse between the
courts of Constantinople and Toledo, appears in the mural
ornamentation of the temples and in the vessels of their shrines, as
well as in the habitations, utensils, and trinkets of the people.
Geometric forms and floral designs—afterwards so popular among
the Moors, who unquestionably derived them largely from this
source—were almost exclusively employed by the Gothic goldsmiths
and architects. Vines, leaves, buds, and quatrefoils enter into almost
every combination in great variety and with charming effect. The
churches were dimly lighted by means of marble slabs pierced with
intersecting cruciform apertures, which increased the mystery and
awe of the interior, devices which are visible to-day in places of
worship as widely separated and of as originally diverse character as
the chapels of the Asturias and the Mosque of Cordova. As soon as
the rage and hatred inspired by the resistance of their enemies—and
which was wreaked upon the edifices of the latter with hardly less
vindictiveness than upon the ranks of their legions—had been
allayed, a desire to profit by the skill and experience of their Roman
subjects became paramount; new structures of simple design and
enduring materials arose in the cities; the ancient monuments were
spared; and the superior state of preservation which distinguishes
the Roman remains in the Peninsula affords incontrovertible
evidence of the enlightened appreciation of the Visigoths.
In the encouragement of the useful and elegant arts, the
Visigoths displayed an enterprising spirit considerably in advance of
the other branches of the great Teutonic nation. Manufactures of
clothing, glass, armor, weapons, thread, and jewelry are known to
have existed in their dominions. But it is in the fabrication of church
furniture, votive offerings, and utensils designed for the service of
the altar, that the labors of their artisans are best known to us. In
the province of Guarrazar, a few miles from Toledo, was accidentally
discovered, in the middle of the last century, a deposit of objects
which had evidently been hastily buried by the priests on the
approach of the Saracen invader. It was composed of a number of
votive crowns—some of which were inscribed with the names of the
donors—sceptres, censers, crosses, candlesticks, lamps, chains,
girdles. All of these were of gold enriched with precious stones. The
ignorance, fear, and avarice of the peasants who discovered this
treasure resulted in the dispersion and loss of the most precious
portion of it; but the crowns were saved, and are now in the Hotel
de Cluny at Paris, and the Royal Armory at Madrid. These articles
enable us to form an excellent idea of the condition of the arts at the
beginning of the eighth century. The accounts given by Christian and
Arab historians of the Visigothic kings, and of the enormous booty
obtained by the Moors, had, until this discovery was made, been
ridiculed by critics as exaggerations, due to the national vanity of
both conquered and conqueror. From even a cursory examination of
these objects—unique in the world—can readily be detected the
taste and style of the Byzantine, whose influence over the artistic
traditions of the Peninsula, far from disappearing with the Gothic
dynasty, was exhibited in some of the most magnificent creations of
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!

ebookfinal.com

You might also like