You are previewing Professional: JavaScript® for Web Developers, Third Edition.

Professional: JavaScript® for Web Developers, Third Edition

  1. Cover
  2. Contents
  3. Introduction
  4. Chapter 1: What is Javascript?
    1. A Short History
    2. JavaScript Implementations
    3. JavaScript Versions
    4. Summary
  5. Chapter 2: Javascript in Html
    1. The <script> Element
    2. Inline Code versus External Files
    3. Document Modes
    4. The <noscript> Element
    5. Summary
  6. Chapter 3: Language Basics
    1. Syntax
    2. Keywords and Reserved Words
    3. Variables
    4. Data Types
    5. Operators
    6. Statements
    7. Functions
    8. Summary
  7. Chapter 4: Variables, Scope, and Memory
    1. Primitive and Reference Values
    2. Execution Context and Scope
    3. Garbage Collection
    4. Summary
  8. Chapter 5: Reference Types
    1. The Object Type
    2. The Array Type
    3. The Date Type
    4. The RegExp Type
    5. The Function Type
    6. Primitive Wrapper Types
    7. Singleton Built-in Objects
    8. Summary
  9. Chapter 6: Object-Oriented Programming
    1. Understanding Objects
    2. Object Creation
    3. Inheritance
    4. Summary
  10. Chapter 7: Function Expressions
    1. Recursion
    2. Closures
    3. Mimicking Block Scope
    4. Private Variables
    5. Summary
  11. Chapter 8: The Browser Object Model
    1. The window Object
    2. The location Object
    3. The Navigator Object
    4. The screen Object
    5. The history Object
    6. Summary
  12. Chapter 9: Client Detection
    1. Capability Detection
    2. Quirks Detection
    3. User-Agent Detection
    4. Summary
  13. Chapter 10: The Document Object Model
    1. Hierarchy of Nodes
    2. Working with the DOM
    3. Summary
  14. Chapter 11: Dom Extensions
    1. Selectors API
    2. Element Traversal
    3. HTML5
    4. Proprietary Extensions
    5. Summary
  15. Chapter 12: Dom Levels 2 and 3
    1. DOM Changes
    2. Styles
    3. Traversals
    4. Ranges
    5. Summary
  16. Chapter 13: Events
    1. Event Flow
    2. Event Handlers
    3. The Event Object
    4. Event Types
    5. Memory and Performance
    6. Simulating Events
    7. Summary
  17. Chapter 14: Scripting Forms
    1. Form Basics
    2. Scripting Text Boxes
    3. Scripting Select Boxes
    4. Form Serialization
    5. Rich Text Editing
    6. Summary
  18. Chapter 15: Graphics With Canvas
    1. Basic Usage
    2. The 2D Context
    3. WebGL
    4. Summary
  19. Chapter 16: Html5 Scripting
    1. Cross-Document Messaging
    2. Native Drag and Drop
    3. Media Elements
    4. History State Management
    5. Summary
  20. Chapter 17: Error Handling and Debugging
    1. Browser Error Reporting
    2. Error Handling
    3. Debugging Techniques
    4. Common Internet Explorer Errors
    5. Summary
  21. Chapter 18: Xml in Javascript
    1. XML DOM Support in Browsers
    2. XPath Support in Browsers
    3. XSLT Support in Browsers
    4. Summary
  22. Chapter 19: Ecmascript For Xml
    1. E4X Types
    2. General Usage
    3. Other Changes
    4. Enabling Full E4X
    5. Summary
  23. Chapter 20: JSON
    1. Syntax
    2. Parsing and Serialization
    3. Summary
  24. Chapter 21: Ajax and Comet
    1. The XMLHttpRequest Object
    2. XMLHttpRequest Level 2
    3. Progress Events
    4. Cross-Origin Resource Sharing
    5. Alternate Cross-Domain Techniques
    6. Security
    7. Summary
  25. Chapter 22: Advanced Techniques
    1. Advanced Functions
    2. Tamper-Proof Objects
    3. Advanced Timers
    4. Custom Events
    5. Drag and Drop
    6. Summary
  26. Chapter 23: Offline Applications and Client-Side Storage
    1. Offline Detection
    2. Application Cache
    3. Data Storage
    4. Summary
  27. Chapter 24: Best Practices
    1. Maintainability
    2. Performance
    3. Deployment
    4. Summary
  28. Chapter 25: Emerging APIS
    1. RequestAnimationFrame()
    2. Page Visibility API
    3. Geolocation API
    4. File API
    5. Web Timing
    6. Web Workers
    7. Summary
  29. Appendix A: Ecmascript Harmony
  30. Appendix B: Strict Mode
  31. Appendix C: Javascript Libraries
  32. Appendix D: Javascript TOOLS
O'Reilly logo

Chapter 2

JavaScript in HTML

WHAT’S IN THIS CHAPTER?

  • Using the <script> element
  • Comparing inline and external scripts
  • Examining how document modes affect JavaScript
  • Preparing for JavaScript-disabled experiences

The introduction of JavaScript into web pages immediately ran into the Web’s predominant language, HTML. As part of its original work on JavaScript, Netscape tried to figure out how to make JavaScript coexist in HTML pages without breaking those pages’ rendering in other browsers. Through trial, error, and controversy, several decisions were finally made and agreed upon to bring universal scripting support to the Web. Much of the work done in these early days of the Web has survived and become formalized in the HTML specification.

THE <SCRIPT> ELEMENT

The primary method of inserting JavaScript into an HTML page is via the <script> element. This element was created by Netscape and first implemented in Netscape Navigator 2. It was later added to the formal HTML specification. There are six attributes for the <script> element:

  • async — Optional. Indicates that the script should begin downloading immediately but should not prevent other actions on the page such as downloading resources or waiting for other scripts to load. Valid only for external script files.
  • charset — Optional. The character set of the code specified using the src attribute. This attribute is rarely used, because most browsers don’t honor its value.
  • defer — Optional. Indicates that the execution of the ...

The best content for your career. Discover unlimited learning on demand for around $1/day.