You are previewing Advanced Javascript, 3rd Edition.
O'Reilly logo
Advanced Javascript, 3rd Edition

Book Description

Advanced JavaScript, Third Edition provides an in-depth examination of the most important features of JavaScript. Beginning with an overview of JavaScript, the book quickly moves into more advanced features needed for complex yet robust JavaScript scripts, such as objects, arrays, and date and time functions. Additionally, various features of JavaScript that are essential for modern web pages are discussed, including manipulating the status bar, creating dynamic calendars, and working with forms, images, and the Document Object Model. Numerous examples illustrate how to implement various techniques. Topics covered how to enhance your web pages with LED signs, banners, and images; implementing cookies to store and retrieve information; the structure of the Document Object Model and how it can be used to view, access, and change an HTML document; Security measures to protect private information while using the Internet.

Table of Contents

  1. Contents Summary
  2. Contents
  3. Acknowledgments
  4. Introduction
    1. The Third Edition
    2. Prerequisites
    3. How to Read This Book
    4. The Structure of This Book
    5. Programming Style
    6. Reading Examples
    7. Encouragement for Nonprogrammers
    8. Warning for Programmers
  5. Chapter 1 Introduction to JavaScript
    1. The World Wide Web
    2. Competition on the Web
    3. Extending HTML
    4. Plug-ins, Applets, and ASP
      1. Plug-ins
      2. Java Applets
      3. ASP and ASP.Net
    5. What Is JavaScript?
    6. Who Can Benefit from JavaScript?
    7. The History of JavaScript
    8. JavaScript in a Browser
      1. Client-side JavaScript
      2. Browser Objects
    9. What Can JavaScript Do?
      1. Image Effects
      2. Games
      3. Banners
      4. Form Validation
      5. Miscellaneous
    10. JavaScript and Java
      1. JavaScript Resembles Java
      2. JavaScript Differs from Java
    11. Summary
  6. Chapter 2 Getting Started with JavaScript
    1. Essential Authoring Tools
      1. Choosing a Text Editor
      2. Choosing a Browser
    2. Basic JavaScript Structure
      1. The <SCRIPT> Tag
        1. Internal Scripts
        2. External Scripts
      2. Event Handlers
      3. Short Statements
      4. Hiding the Script for Old Browsers
    3. Important Notes about JavaScript
    4. Summary
  7. Chapter 3 JavaScript Fundamentals
    1. “Building” a Script
    2. Data Types in JavaScript
      1. The Value Null
      2. Variables
      3. Identifiers
      4. Naming Conventions
      5. Variable Declaration
    3. JavaScript Entities
    4. Type Conversion
    5. Mixing Strings and Numbers
    6. Literals
      1. Integer Literals
        1. Decimal Integers
        2. Octal Integers
        3. Hexadecimal Integers
        4. Converting from Decimal to Other Bases
        5. Converting from a Specified Base to Decimal
        6. Referring to Octal and Hexadecimal Integers
      2. Floating-Point Literals
      3. Boolean Literals
      4. String Literals
    7. Operators
    8. Statements
      1. Multiple Statements
      2. Nested Statements
    9. Evaluating Expressions
    10. Testing Evaluation
    11. Functions
      1. Function Definition
      2. Calling Functions
    12. Control Structures
      1. Dialog Boxes
        1. Alert Boxes
        2. Confirm Boxes
        3. Prompt Boxes
      2. Conditional Statements
        1. if Statement
        2. else Statement
        3. Nested if-else Statements
      3. Loop Statements
        1. for Statement
        2. while Statement
    13. Summary
  8. Chapter 4 Object-Oriented Programming
    1. Objects
    2. Properties
      1. Syntax
    3. Methods
      1. Syntax
    4. Using the Array Notation
    5. Object Oriented vs. Object Based
    6. Summary
  9. Chapter 5 Functions and Variable Scope
    1. Variable Scope and Storage Class
      1. Variable Scope
      2. Variable Storage Class
      3. Function Parameters
    2. What Are Functions?
      1. Defining and Calling Functions
      2. Function Parameters
      3. Using the arguments Array
      4. Creating Functions with Default Arguments
    3. Returning a Value
    4. Recursion
    5. Summary
  10. Chapter 6 Utilizing JavaScript Operators
    1. Operator Categories
    2. Mathematical Operators
      1. Addition
      2. Subtraction
      3. Multiplication
      4. Division
      5. Modulus
      6. The Nonexistent Integral Division Operator
      7. Increment
      8. Decrement
      9. Negation
    3. String Concatenation
    4. Bitwise Operators
      1. Bitwise AND
      2. Bitwise OR
      3. Bitwise XOR
      4. Bitwise NOT
      5. Shift Operators
        1. Left Shift
        2. Right Shift
        3. Zero-fill Right Shift
    5. Assignment Operators
    6. Relational Operators
      1. Equality and Inequality
    7. Short-Circuit Logical Operators
      1. Logical OR
      2. Logical AND
      3. Logical NOT
      4. Testing Equality for Floating-Point Numbers
    8. More Logical Operators
      1. Conditional Operator
      2. Comma Operator
    9. Data Type Operator
    10. Void Operator
    11. Operator Precedence
    12. Expressions
    13. Side Effects
    14. Summary
  11. Chapter 7 Events in JavaScript
    1. Events
    2. Event Handlers
      1. onLoad and onUnload
      2. onError
    3. Emulating Events via Methods
    4. Calling Event Handlers Explicitly
    5. Canceling Events
    6. Summary
  12. Chapter 8 Building and Extending Objects
    1. Creating Instances
    2. Constructor Functions
      1. Defining Object Properties
    3. Nested Objects
      1. Defining Methods
      2. Creating Objects—An Example
    4. Prototype and Object Extensions
    5. Summary
  13. Chapter 9 Arrays
    1. What Is an Array?
    2. Some History
    3. Creating Simple Arrays
    4. Referring to an Array’s Elements
    5. Creating Dense Arrays
    6. Array Types
    7. Array Properties
    8. Array Methods
      1. chop()
      2. grep()
      3. join()
      4. pop()
      5. push()
      6. reverse()
      7. shift()
      8. sort()
      9. splice()
      10. split()
      11. unshift()
    9. The arguments Array
    10. Multidimensional Arrays
      1. Creation with a Single Constructor Function
      2. Creation without a Constructor Function
    11. Associative Arrays
      1. Populating an Associative Array
      2. Splitting a String into an Associative Array
    12. Summary
  14. Chapter 10 Time and Date in JavaScript
    1. The Date Object
      1. Creating a Date Instance
      2. Parameters of the Date Constructor
    2. Date Numeric Conventions
    3. Date Method Categories
    4. The get Methods
      1. getYear()
      2. getMonth()
      3. getDate()
      4. getDay()
      5. getHours()
      6. getMinutes()
      7. getSeconds()
      8. getTimezoneOffset()
      9. getTime()
    5. The set Methods
      1. setYear()
      2. setMonth()
      3. setDate()
      4. setHours()
      5. setMinutes()
      6. setSeconds()
      7. setTime()
    6. The to Methods
      1. toGMTString()
      2. toLocaleString()
    7. The parse Methods
      1. parse()
      2. UTC()
    8. Time-Related Methods of Other Objects
      1. setTimeout()
      2. clearTimeout()
    9. Time and Date Examples
      1. A Simple Digital Clock
      2. Monthly Calendar
        1. getTime()
        2. leapYear(year)
        3. getDays(month, year)
        4. getMonthName(month)
        5. setCal()
        6. drawCal(firstDay, lastDate, date, monthName, year)
      3. Random Quotes
    10. Summary
  15. Chapter 11 JavaScript Math
    1. Math in JavaScript
    2. The Math Object
    3. Constants
      1. E
      2. LN2
      3. LN10
      4. LOG2E
      5. LOG10E
      6. PI
      7. SQRT1_2
      8. SQRT2
    4. Math Methods
      1. Arithmetic Methods
        1. abs()
        2. ceil()
        3. exp()
        4. floor()
        5. log()
        6. max(), min()
        7. pow()
        8. random()
        9. round()
        10. sqrt()
      2. Trigonometric Methods
        1. cos()
        2. acos()
        3. sin()
        4. asin()
        5. tan()
        6. atan()
        7. atan2()
    5. The Number Object
      1. Number Properties
        1. MAX_VALUE
        2. MIN_VALUE
        3. NaN
        4. NEGATIVE_INFINITY
        5. POSITIVE_INFINITY
      2. Number Methods
      3. Math-Related Functions
        1. parseInt()
        2. parseFloat()
        3. eval()
    6. Math Examples
      1. Creating a Curve
        1. drawBlank(num)
        2. drawDot()
        3. getRadian(deg)
        4. getSpot(deg)
        5. get3DigitNum(num)
        6. printDeg(deg)
        7. drawLine(deg)
        8. drawCurve(lastDeg, jump)
        9. Global Statements
      2. General Plotting Utility
        1. root(a, b)
        2. logab(a, b)
        3. factorial(a)
        4. startWizard()
        5. drawBlank(num)
        6. drawDot(x, y)
        7. replaceSpecialSequence(str)
        8. splitFunc(func)
        9. getInput()
        10. yVal(xVal)
        11. makeArray(min, max, dif)
        12. printUnit(num)
        13. drawCurve()
        14. main()
        15. Global Statements
    7. Summary
  16. Chapter 12 Handling Strings
    1. Characters
    2. Creating Strings
    3. String Length
    4. HTML Formatting Methods
    5. General String Methods
      1. charAt()
      2. indexOf()
      3. lastIndexOf()
      4. substring()
    6. escape() and unescape()
    7. Number-to-String Conversion
      1. Empty String Concatenation
      2. String Instance Construction
      3. The toString() Method
    8. String-to-Number Conversion
      1. parseInt() and parseFloat()
      2. Determining if a Value is a Number or Not
    9. Evaluating Text Expressions
    10. String Handling Example
      1. String Enciphering
      2. encipher()
      3. encode(str, key)
    11. Summary
  17. Chapter 13 The Document Object Model
    1. The window Object
    2. The navigator Object
    3. The document Object
    4. The history Object
    5. The location Object
    6. Summary
  18. Chapter 14 The Document Object Model (Continued)
    1. Accessing a Document’s Structure with the DOM
    2. Using the Document Object Model
    3. Summary
  19. Chapter 15 Utilizing the Status Bar
    1. The Status Bar
    2. Writing to the Status Bar
    3. Setting a Default Value to the Status Bar
    4. Banners
      1. T-banner
        1. Global Statements
        2. stopBanner()
        3. startBanner()
        4. showBanner()
        5. Event Handlers
      2. R-banner
        1. Global Statements
        2. clearState()
        3. stopBanner()
        4. startBanner()
        5. clearState()
        6. getRandom(max)
        7. getString()
        8. showBanner()
        9. Event Handlers
      3. N-banner
        1. scrollBanner(seed)
    5. Summary
  20. Chapter 16 URLs and JavaScript
    1. A Crash Course in URLs
      1. General URL Syntax
      2. The URL Schemes
        1. Hypertext Transfer Protocol (HTTP)
        2. File Transfer Protocol (FTP)
        3. Gopher Protocol (Gopher)
        4. Electronic Mail (Mailto)
        5. Usenet News (News)
        6. Host-Specific Filenames (File)
        7. JavaScript Statements (javascript)
        8. Other Schemes
    2. The location Object
    3. location Properties
      1. href
      2. hash
      3. host
      4. hostname
      5. pathname
      6. port
      7. protocol
      8. search
    4. location Methods
      1. reload
      2. replace
    5. Another Location—document.location (document.URL)
    6. Search Utilities
    7. Summary
  21. Chapter 17 The Document Object and the History List
    1. The document Object
      1. The title Property
    2. Colors
      1. bgColor
        1. Global Statements
        2. display(triplet)
        3. drawCell(red, green, blue)
        4. drawRow(red, blue)
        5. drawTable(blue)
        6. drawCube()
        7. toHex(dec)
        8. setbgColor(red, green, blue)
        9. fade(sred, sgreen, sblue, ered, egreen, eblue, step)
        10. Event Handlers
      2. fgColor
      3. alinkColor
      4. linkColor
      5. vlinkColor
    3. Output Methods and Streams
      1. write and writeln
      2. Data Streams
    4. What Is a History List?
    5. The history Object
    6. History List Length
    7. History List Entry Indexing
    8. history Methods
      1. back
      2. forward
      3. go
    9. Security Aspects of the history Object
    10. Summary
  22. Chapter 18 Forms
    1. What Are HTML Forms?
    2. JavaScript Form Reference
      1. forms Array
      2. Form Name
    3. form Object
      1. HTML Syntax
      2. Event Handlers
        1. onSubmit
        2. onReset
      3. Methods
        1. submit()
        2. reset()
      4. Properties
        1. action
        2. elements
        3. encoding
        4. method
        5. target
      5. Element Types
      6. Using this with Event Handlers
      7. Utilizing the form Property
    4. text Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
        1. onBlur
        2. onChange
        3. onFocus
        4. onSelect
      4. Methods
        1. blur()
        2. focus()
        3. select()
      5. Properties
        1. defaultValue
        2. name
        3. value
    5. password Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
      4. Properties and Methods
    6. textarea Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
      4. Properties and Methods
      5. Inserting New Line Characters
      6. Handling textareas by Line
    7. hidden Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
      4. Properties and Methods
    8. button, submit, and reset Objects
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
        1. onClick
      4. Methods
        1. click()
      5. Properties
        1. name
        2. value
    9. checkbox Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
        1. onClick
      4. Methods
        1. click()
      5. Properties
        1. checked
        2. defaultChecked
        3. name
        4. value
    10. radio Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
        1. onClick
      4. Methods
        1. click()
      5. Properties
        1. checked
        2. defaultChecked
        3. name
        4. value
    11. select Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers
        1. onBlur
        2. onChange
        3. onFocus
      4. Methods
        1. blur, focus
      5. Properties (select Object)
        1. length
        2. name
        3. options
        4. selectedIndex
      6. Properties (options Array)
        1. defaultSelected
        2. index
        3. length
        4. selected
        5. text
        6. value
      7. The Option Object—Adding Options Using the Option Constructor
    12. fileUpload Object
      1. HTML Syntax
      2. JavaScript Access
      3. Event Handlers and Methods
      4. Properties
        1. name
        2. value
    13. Summary
  23. Chapter 19 Links, Anchors, and Image Maps
    1. Defining a Link
    2. Defining an Anchor
    3. Defining an Image Map Area
    4. links Array
    5. anchors Array
    6. Link and Area Event Handlers
      1. Calling Event Handlers Explicitly
      2. onClick
      3. onMouseOver
      4. onMouseOut
    7. Link and Area Properties
      1. Location-Equivalent Properties
      2. target
    8. Referring Documents
    9. Summary
  24. Chapter 20 Implementing Cookies
    1. Maintaining a State
    2. Cookies and HTTP
      1. Setting an HTTP Cookie
      2. Getting an HTTP Cookie
      3. Notes and Limitations
      4. Examples
        1. First Transaction Sequence Example
        2. Second Transaction Sequence Example
    3. Cookies and JavaScript
    4. Cookie Functions
      1. getInput(num, monthName)
      2. initCookie(monthName)
      3. getSpecificReminder(num, monthName)
      4. setSpecificReminder(num, monthName, newValue)
    5. Outliner
      1. item(parent, text, depth)
      2. makeArray(length)
      3. makeDatabase()
      4. setStates()
      5. setImages()
      6. toggle(num)
      7. setStorage()
      8. Global Statements
    6. Summary
  25. Chapter 21 Images and Graphics
    1. Defining Images in HTML
    2. The image Object
      1. The images Array
      2. Properties
        1. border
        2. complete
        3. height
        4. hspace
        5. lowsrc
        6. name
        7. src
        8. vspace
        9. width
      3. Event Handlers
        1. onAbort
        2. onError
        3. onLoad
    3. Demonstration 1: Updating Digital Clock
      1. Global Statements
      2. setClock()
      3. update()
      4. stopClock()
      5. getHour(place)
      6. getMinute(place)
      7. getAmpm()
      8. getPath(url)
    4. Demonstration 2: LED Sign
      1. Global Statements
      2. letterArray()
      3. drawBlank()
      4. setLight(state, x, y)
      5. drawLetter(letter, startX)
      6. drawSpace(startX)
      7. computeIndex()
      8. floodBoard(startX)
      9. drawMessage(num)
      10. startSign()
      11. stopSign()
      12. More Global Statements
    5. Demonstration 3: Netris Deluxe
      1. Global Statements
      2. drawScreen()
      3. computeIndex(x, y)
      4. state(x, y)
      5. setSquare(x, y, state)
      6. clearActive()
      7. checkMoveX(step)
      8. checkMoveY()
      9. moveX(step)
      10. smartX(x, y, step)
      11. moveY()
      12. smartY(x, y)
      13. shapeMap()
      14. getRandom()
      15. insertShape()
      16. complexWarp()
      17. checkWarp(startX, startY, endX, endY)
      18. rotate()
      19. flood(state)
      20. noActive()
      21. isLine(y)
      22. warp(startX, startY, endX, endY)
      23. start()
      24. dropLines()
      25. play()
      26. characteristics(x, y)
      27. fullMap()
      28. pause()
      29. Music
    6. Summary
  26. Chapter 22 Frames
    1. What Are Frames?
    2. Creating Frames
    3. Targeting Frames
    4. Nested Frames
    5. JavaScript and Frames
    6. self
    7. parent
    8. top
    9. frames
    10. An Example: The Color Center
      1. The Frame-setting Window
        1. display(r, g, b)
        2. makeAttribute(r, g, b)
        3. Global Statements
        4. select(r, g, b)
        5. curAttribute()
        6. bodyDefinition()
        7. update()
        8. save()
        9. load()
      2. HTML
      3. The “control” Frame
        1. display()
        2. Event Handlers
      4. The “swatches” Frame
      5. The “main” Frame
    11. Frames, Events, and Event Handlers
      1. Using Event Handlers in a Frame
      2. Using Event Handlers in a Frame-Setting Document
        1. onFocus and onBlur
      3. Emulating Events
    12. Targeting Multiple Frames
    13. Summary
  27. Chapter 23 Evaluation and Compilation
    1. Evaluating a String
    2. Function References and Calls
    3. Compiling Code as a Function
      1. Specifying the Function’s Body
      2. Using the function Object with a Variable
      3. Specifying Arguments
      4. Using the function Object with an Event Handler
      5. Properties of the function Object
    4. Summary
  28. Chapter 24 JavaScript Authoring Tools
    1. Introduction
    2. ScrypTik
    3. Komodo
    4. 1st JavaScript Editor Pro
    5. Summary
  29. Chapter 25 Plug-ins
    1. Embedding a Plug-in Object in HTML
    2. Referencing Plug-ins in JavaScript
    3. Determining Installed Plug-ins with JavaScript
      1. Properties of the mimeTypes Object
      2. Properties of the plugins Object
    4. LiveAudio and LiveVideo
      1. LiveAudio
      2. LiveVideo
      3. Other Plug-ins
    5. Summary
  30. Chapter 26 Style Sheets
    1. Introduction
    2. Content Layout
    3. Font Properties
    4. Text Properties
    5. Inheritance of Styles
    6. Creating Style Sheets and Assigning Styles
      1. Defining Styles with the <STYLE> Tag in the Header
      2. Specifying Styles for Individual Elements
      3. Defining Classes of Styles
    7. Format Properties
      1. Box Math
      2. Replaced Elements
      3. Setting Margins
      4. Setting Border Width
      5. Setting the Padding Size
    8. Summary
  31. Chapter 27 Security Issues
    1. History
    2. URL to URL Access Security
    3. The Concept of Tainting
      1. Enabling Data Tainting
      2. Specific Tainting
      3. Window Taint Accumulator
    4. Summary
  32. Chapter 28 Debugging Scripts
    1. Types of Errors
    2. Error Messages
      1. “string is not defined”
      2. “string is not a function”
      3. “unterminated string literal”
      4. “missing } after function body”
      5. “string is not a number”
      6. “string has no property named property”
      7. “string has no property indexed by [i]”
      8. “string cannot be set by assignment”
      9. “test for equality (==) mistyped as assignment (=)? Assuming equality test”
      10. “function does not always return a value”
      11. “access disallowed from scripts at URL_1 to documents at URL_2”
      12. “Lengthy JavaScript still running. Continue?”
      13. “syntax error”
    3. Manual Debugging Techniques
      1. Match Those Tag Pairs
      2. View the Intermediate HTML Source
      3. Reopen the File
      4. Print Messages
      5. Comment Out Statements
      6. Watching and Tracing Variables
    4. Getting It Right the First Time
      1. Build the Script’s Skeleton First
      2. Keep Testing While Developing
      3. Evaluate Expressions Outside Your Source
      4. Test Your Functions Outside Your Source
    5. Testing Your Script
    6. Summary
  33. Appendix A HTML Primer
    1. HTML Tags
  34. Appendix B JavaScript Object Specification Syntax
    1. anchor Object
      1. Creating
    2. applet Object
      1. Creating
      2. Properties
      3. Event Handlers
      4. Methods
    3. area Object
      1. Creating
      2. Properties
      3. Event Handlers
    4. Array Object
      1. Creating
      2. Properties
      3. Methods
    5. button, submit, and reset Objects
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    6. checkbox Object
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    7. Date Object
      1. Creating
      2. Properties
      3. Methods
    8. document Object
      1. Creating
      2. Properties
      3. Methods
    9. fileUpload Object
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    10. form Object
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    11. function Object
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    12. hidden Object
      1. Creating
      2. Properties
    13. history Object
      1. Properties
      2. Methods
    14. image Object
      1. Creating
      2. Properties
      3. Event Handlers
    15. layer Object
      1. Properties
      2. Methods
    16. link Object
      1. Creating
      2. Properties
      3. Event Handlers
    17. Math Object
      1. Properties
      2. Methods
    18. mimeType Object
      1. Properties
    19. navigator Object
      1. Properties
      2. Methods
    20. plugin Object
      1. Properties
      2. Methods
    21. radio Object
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    22. select Object
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    23. String Object
      1. Creating
      2. Properties
      3. Methods
    24. text, textarea, and password Objects
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
    25. window and frame Objects
      1. Creating
      2. Properties
      3. Methods
      4. Event Handlers
      5. Control Structures
    26. Operators
      1. Comparison
      2. Binary and Unary
      3. Assignment
      4. Boolean
      5. Bitwise
      6. Miscellaneous
      7. JavaScript Functions and Methods
  35. Appendix C ASCII Character Set
  36. Appendix D Online Resources
    1. JavaScript Web Sites
    2. HTML Web Sites
    3. Organizational Web Sites
    4. Certification Web Sites
    5. Employment Web Sites
  37. Index