You are previewing JavaScript A Beginners Guide 4/E.
O'Reilly logo
JavaScript A Beginners Guide 4/E

Book Description

Fully updated for the latest JavaScript standard and featuring a new chapter on HTML5 and jQueryJavaScript: A Beginner’s Guide shows how to create dynamic Web pages complete with special effects using today’s leading Web development language. With the growth of HTML 5, JavaScript is expected to grow even more to script the canvas element, add drag and drop functionality, and more.

Table of Contents

  1. Cover 
  2. Title Page
  3. Copyright Page
  4. About the Author
  5. Contents at a Glance
  6. Contents 
  7. Acknowledgments
  8. Introduction
  9. 1 Introduction to JavaScript
    1. What You Need to Know
      1. Basic HTML and CSS Knowledge
      2. Basic Text Editor and Web Browser Knowledge
      3. Which Version?
      4. Remember, It’s Not Java
      5. Similarities to Other Languages
    2. Beginning with JavaScript
      1. Prototype-Based
      2. Client-Side
      3. Scripting Language
    3. Putting It All Together
    4. Online Resources
    5. Try This 1-1: Use JavaScript to Write Text
    6. Chapter 1 Self Test
  10. 2 Placing JavaScript in an HTML File
    1. Using the HTML Script Tags
      1. Identifying the Scripting Language
      2. Calling External Scripts
      3. Specifying When the Script Should Load
      4. Using <noscript></noscript> Tags
    2. Creating Your First Script
      1. Writing a “Hello World” Script
      2. Creating an HTML Document for the Script
      3. Inserting the Script into the HTML Document
    3. Try This 2-1: Insert a Script into an HTML Document
    4. Using External JavaScript Files
      1. Creating a JavaScript File
      2. Creating the HTML Files
      3. Viewing the Pages in Your Browser
    5. Try This 2-2: Call an External Script from an HTML Document
    6. Using JavaScript Comments
      1. Inserting Comments on One Line
      2. Adding Multiple-Line Comments
    7. Chapter 2 Self Test
  11. 3 Using Variables
    1. Understanding Variables
    2. Why Variables Are Useful
      1. Variables as Placeholders for Unknown Values
      2. Variables as Time-Savers
      3. Variables as Code Clarifiers
    3. Defining Variables for Your Scripts
      1. Declaring Variables
      2. Assigning Values to Variables
      3. Naming Variables
    4. Understanding Data Types
      1. Number
      2. String
      3. Boolean
      4. Null
      5. Undefined
    5. Try This 3-1: Declare Variables
    6. Using Variables in Scripts
      1. Making a Call to a Variable
      2. Adding Variables to Text Strings
    7. Writing a Page of JavaScript
      1. Creating the Framework
      2. Defining the Variables
      3. Adding the Commands
      4. Modifying the Page
    8. Try This 3-2: Create an HTML Page with JavaScript
    9. Chapter 3 Self Test
  12. 4 Using Functions
    1. What a Function Is
    2. Why Functions Are Useful
    3. Structuring Functions
      1. Declaring Functions
      2. Defining the Code for Functions
      3. Naming Functions
      4. Adding Arguments to Functions
      5. Adding Return Statements to Functions
    4. Calling Functions in Your Scripts
      1. Script Tags: Head Section or Body Section
      2. Calling a Function from Another Function
      3. Calling Functions with Arguments
      4. Calling Functions with Return Statements
      5. Other Ways to Define Functions
    5. Try This 4-1: Create an HTML Page with Functions
    6. Scope/Context Basics
      1. Global Context
      2. Function Context
    7. Try This 4-2: Write Your Own Functions
    8. Chapter 4 Self Test
  13. 5 JavaScript Operators
    1. Understanding the Operator Types
    2. Understanding Arithmetic Operators
      1. The Addition Operator (+)
      2. The Subtraction Operator (–)
      3. The Multiplication Operator (*)
      4. The Division Operator (/)
      5. The Modulus Operator (%)
      6. The Increment Operator (++)
      7. The Decrement Operator (– –)
      8. The Unary Plus Operator (+)
      9. The Unary Negation Operator (–)
    3. Understanding Assignment Operators
      1. The Assignment Operator (=)
      2. The Add-and-Assign Operator (+=)
      3. The Subtract-and-Assign Operator (–=)
      4. The Multiply-and-Assign Operator (*=)
      5. The Divide-and-Assign Operator (/=)
      6. The Modulus-and-Assign Operator (%=)
    4. Try This 5-1: Adjust a Variable Value
    5. Understanding Comparison Operators
      1. The Is-Equal-To Operator (==)
      2. The Is-Not-Equal-To Operator (!=)
      3. The Strict Is-Equal-To Operator (===)
      4. The Strict Is-Not-Equal-To Operator (!==)
      5. The Is-Greater-Than Operator (>)
      6. The Is-Less-Than Operator (<)
      7. The Is-Greater-Than-or-Equal-To Operator (>=)
      8. The Is-Less-Than-or-Equal-To Operator (<=)
    6. Understanding Logical Operators
      1. The AND Operator (&&)
      2. The OR Operator (||)
      3. The NOT Operator (!)
      4. The Bitwise Operators
    7. Special Operators
    8. Understanding Order of Operations
    9. Try This 5-2: True or False?
    10. Chapter 5 Self Test
  14. 6 Conditional Statements and Loops
    1. Defining Conditional Statements
      1. What Is a Conditional Statement?
      2. Why Conditional Statements Are Useful
    2. Using Conditional Statements
      1. Using if/else Statements
      2. Using the switch Statement
      3. Using the Conditional Operator
      4. User Input from a Prompt
    3. Try This 6-1: Work with User Input
    4. Defining Loops
      1. What Is a Loop?
      2. Why Loops Are Useful
    5. Using Loops
      1. for
      2. while
      3. do while
      4. for in and for each in
      5. Using break and continue
    6. Try This 6-2: Work with for Loops and while Loops
    7. Chapter 6 Self Test
  15. 7 JavaScript Arrays
    1. What Is an Array?
    2. Why Arrays Are Useful
    3. Defining and Accessing Arrays
      1. Naming an Array
      2. Defining an Array
      3. Accessing an Array’s Elements
      4. Using the length Property and Loops
      5. Changing Array Values and Changing the Length
    4. Try This 7-1: Use Loops with Arrays
    5. Array Properties and Methods
      1. Properties
      2. Methods
    6. Nesting Arrays
      1. Defining Nested Arrays
      2. Loops and Nested Arrays
    7. Try This 7-2: Nested Arrays Practice
    8. Chapter 7 Self Test
  16. 8 Objects
    1. Defining Objects
    2. Creating Objects
      1. Naming
      2. Single Objects
    3. Try This 8-1: Create a Computer Object
    4. Object Structures
      1. Constructor Functions
      2. Using Prototypes
    5. Helpful Statements for Objects
      1. The for-in Loop
      2. The with Statement
    6. Try This 8-2: Practice with the Combination Constructor/Prototype Pattern
    7. Understanding Predefined JavaScript Objects
      1. The Navigator Object
      2. The History Object
    8. Chapter 8 Self Test
  17. 9 The Document Object
    1. Defining the Document Object
    2. Using the Document Object Model
    3. Using the Properties of the Document Object
      1. Collections
      2. The cookie Property
      3. The dir Property
      4. The lastModified Property
      5. The referrer Property
      6. The title Property
      7. The URL Property
      8. The URLUnencoded Property
    4. Using the Methods of the Document Object
      1. The get Methods for Elements
      2. The open() and close() Methods
      3. The write() and writeln() Methods
    5. Using DOM Nodes
      1. DOM Node Properties
      2. DOM Node Methods
    6. Try This 9-1: Add a DOM Node to the Document
    7. Creating Dynamic Scripts
      1. Styles in JavaScript
      2. Simple Event Handling
      3. Coding a Dynamic Script
    8. Try This 9-2: Try Out Property Changes
    9. Chapter 9 Self Test
  18. 10 Event Handlers
    1. What Is an Event Handler?
    2. Why Event Handlers Are Useful
    3. Understanding Event Handler Locations and Uses
      1. Using an Event Handler in an HTML Element
      2. Using an Event Handler in the Script Code
    4. Learning the Events
      1. The Click Event
      2. Focus and Blur Events
      3. The Load and Unload Events
      4. The Reset and Submit Events
      5. The Mouse Events
      6. The Keyboard Events
    5. Try This 10-1: Focus and Blur
    6. Other Ways to Register Events
      1. The addEventListener() Method
      2. The attachEvent() Method
    7. The Event Object
      1. DOM and Internet Explorer: DOM Level 0 Registration
      2. Using event with Modern Event Registration
      3. Properties and Methods
      4. Event Information
    8. Try This 10-2: Using addEventListener()
    9. Creating Scripts Using Event Handlers
      1. Show Hidden Content
      2. Change Content
    10. Chapter 10 Self Test
  19. 11 Window Object
    1. Window: The Global Object
    2. Using the Properties of the Window Object
      1. The closed Property
      2. The frames Property
      3. The innerWidth and innerHeight Properties
      4. The length Property
      5. The location Property
      6. The name Property
      7. The opener Property
      8. The parent, self, and top Properties
      9. The status and defaultStatus Properties
    3. Try This 11-1: Use the location and innerWidth Properties
    4. Using the Methods of the Window Object
      1. The alert(), prompt(), and confirm() Methods
      2. The find() Method
      3. The home() Method
      4. The print() Method
      5. The setInterval() and clearInterval() Methods
      6. The setTimeout() and clearTimeout() Methods
    5. Try This 11-2: Use the setTimeout() and confirm() Methods
    6. The Main Window and New Windows
      1. The Tale of Pop-up Windows
      2. Opening New Windows
      3. Closing New Windows
      4. Moving, Resizing, and Scrolling New Windows
      5. The resizeBy() and resizeTo() Methods
      6. The scrollBy() and ScrollTo() Methods
    7. Chapter 11 Self Test
  20. 12 Math, Number, and Date Objects
    1. Using the Math Object
      1. What Is the Math Object?
      2. How the Math Object Is Useful
      3. Properties
      4. Methods
    2. Try This 12-1: Display a Random Link on a Page
    3. Understanding the Number Object
      1. Properties
      2. Methods
    4. Using the Date Object
      1. Properties and Methods
      2. Methods That Get Values
      3. Methods That Set Values
      4. Other Methods
      5. How About Some Date Scripts?
    5. Try This 12-2: Create a JavaScript Clock
    6. Chapter 12 Self Test
  21. 13 Handling Strings
    1. Introduction to the String Object
      1. The String Object
      2. The String Literal
      3. What’s the Difference?
    2. Using the Properties and Methods of the String Object
      1. The length Property
    3. Methods of the String Object
    4. Try This 13-1: Use indexOf() to Test an Address
    5. Using Cookies
      1. Setting a Cookie
      2. Reading a Cookie
    6. Try This 13-2: Remember a Name
    7. Using Regular Expressions
      1. Creating Regular Expressions
      2. Testing Strings Against Regular Expressions
      3. Adding Flags
      4. Creating Powerful Patterns
      5. Grouping Expressions
      6. The replace(), match(), and search() Methods
      7. More Information
    8. Chapter 13 Self Test
  22. 14 JavaScript and Forms
    1. Accessing Forms
      1. Using the forms Array
      2. Using Form Names
      3. Using an ID
    2. Using the Properties and Methods of the Form Object
      1. Properties
      2. Methods
    3. Ensuring the Accessibility of Forms
      1. Using Proper Element and Label Order
      2. Using <label></label> Tags
      3. Using <fieldset></fieldset> Tags
      4. Not Assuming Client-Side Scripting
    4. Validation
      1. Simple Validation
      2. Techniques
      3. Check Boxes and Radio Buttons
    5. Try This 14-1: Request a Number
    6. HTML5 and Forms
      1. New Elements
      2. New Input Types
      3. New Attributes
      4. HTML5 Form Validation
    7. Try This 14-2: Validate a Phone Number with HTML5 or JavaScript
    8. Chapter 14 Self Test
  23. 15 An Introduction to Advanced Techniques
    1. Working with Images
      1. Rollovers
    2. Try This 15-1: A More Accessible Rollover
    3. JavaScript and Frames
      1. Purpose of Frames
      2. Accessing Frames
      3. Breaking Out of Frames
    4. Debugging Scripts
      1. Types of Errors
      2. Alerts and Using the Console
      3. Using a Lint Tool
      4. Browser Developer Tools
    5. JavaScript and Accessibility
      1. Separate Content from Presentation
      2. Enhancing Content
    6. Try This 15-2: Make This Code Accessible
    7. JavaScript Security
      1. Security and Signed Scripts
      2. Page Protection
    8. AJAX and JSON
      1. AJAX
      2. JSON
    9. Chapter 15 Self Test
  24. 16 JavaScript Libraries, HTML5, and Harmony
    1. Using jQuery
      1. Obtaining jQuery
      2. Getting Started: document.ready()
      3. Using Selectors
      4. Altering Classes
      5. Methods for Effects
      6. Further Reading
    2. Try This 16-1: Using jQuery to Create Effects
    3. Other JavaScript Libraries
      1. jQuery Mobile
      2. php.js
      3. node.js
      4. MooTools
      5. Three.js
    4. JavaScript and HTML5
      1. The <canvas> Element
      2. Drag and Drop
    5. Try This 16-2: Drag and Drop
    6. ECMAScript Harmony
      1. The const and let Keywords
      2. Default Argument Values
      3. Classes
      4. More on Harmony
    7. Further Reading
      1. Need Help?
    8. Chapter 16 Self Test
  25. A Answers to Self Tests
    1. Chapter 1: Introduction to JavaScript
    2. Chapter 2: Placing JavaScript in an HTML File
    3. Chapter 3: Using Variables
    4. Chapter 4: Using Functions
    5. Chapter 5: JavaScript Operators
    6. Chapter 6: Conditional Statements and Loops
    7. Chapter 7: JavaScript Arrays
    8. Chapter 8: Objects
    9. Chapter 9: The Document Object
    10. Chapter 10: Event Handlers
    11. Chapter 11: Window Object
    12. Chapter 12: Math, Number, and Date Objects
    13. Chapter 13: Handling Strings
    14. Chapter 14: JavaScript and Forms
    15. Chapter 15: An Introduction to Advanced Techniques
    16. Chapter 16: JavaScript Libraries, HTML5, and Harmony
  26. Index