Sams Teach Yourself JavaScript in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Part I: Getting Started
- Hour 1. Understanding JavaScript
- Learning Web Scripting Basics
- How JavaScript Fits into a Web Page
- Browsers and JavaScript
- Alternatives to JavaScript
- Summary
- Q&A
- Quiz
- Exercises
- Hour 2. Creating a Simple Script
- Hour 3. How JavaScript Programs Work
- Part II: Learning JavaScript Basics
- Hour 4. Using Functions and Variables
- Hour 5. Using Strings and Arrays
- Hour 6. Testing and Comparing Values
- Hour 7. Repeating Yourself: Using Loops
- Hour 8. Using Math and Date Functions
- Part III: The Document Object Model (DOM)
- Hour 9. Working with the Document Object Model
- Hour 10. Responding to Events
- Hour 11. Using Windows and Frames
- Hour 12. Getting Data with Forms
- Hour 13. Using Graphics and Animation
- Part IV: Moving on to Advanced JavaScript Features
- Hour 14. Creating Cross-Browser Scripts
- Hour 15. Creating Custom Objects
- Hour 16. Working with Sounds and Plug-Ins
- Hour 17. Debugging JavaScript Applications
- Part V: Working with Dynamic HTML (DHTML)
- Hour 18. Working with Style Sheets
- Hour 19. Using Dynamic HTML (DHTML)
- Hour 20. Using Advanced DOM Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Hour 23. Creating DHTML Applications
- Hour 24. JavaScript Tips and Tricks
- Part VII: Appendices
- Appendix A. Other JavaScript Resources
- Appendix B. Tools for JavaScript Developers
- Appendix C. Glossary
- Appendix D. JavaScript Quick Reference
- Appendix E. DOM Quick Reference
Learning Web Scripting Basics
In the world of science fiction movies (and many other movies that have no excuse), computers are often seen obeying commands in English. While this may indeed happen in the near future, computers currently find it easier to understand languages like BASIC, C, and Java.
If you know how to use HTML to create a Web document, you've already worked with one computer language. You use HTML tags to describe how you want your document formatted, and the browser obeys your commands and shows the formatted document to the user.
Since HTML is a simple text markup language, it can't respond to the user, make decisions, or automate repetitive tasks. Interactive tasks like these require a more complex language: a programming language, or a scripting language.
While many programming languages are complex, scripting languages are generally simple. They have a simple syntax, can perform tasks with a minimum of commands, and are easy to learn. Web scripting languages allow you to combine scripting with HTML to create interactive Web pages.
Scripts and Programs
A movie or play follows a script—a list of actions (or lines) for the actors to perform. A Web script provides the same type of instructions for the browser. A script in JavaScript can range from a single line to a full-scale application. (In either case, JavaScript scripts almost always run within a browser.)
Some programming languages must be compiled, or translated into machine code, before they can be executed. JavaScript, on the other hand, is an interpreted language: The browser executes each line of script as it comes to it.
There is one main advantage to interpreted languages: writing or changing a script is very simple. Changing a JavaScript script is as easy as changing a typical HTML document, and the change is enacted as soon as you reload the document in the browser.
Introducing JavaScript
JavaScript was developed by Netscape Communications Corporation, the makers of the popular Netscape Web browser. JavaScript was the first Web scripting language to be introduced, and it is still by far the most popular.
JavaScript is almost as easy to learn as HTML, and it can be included directly in HTML documents. Here are a few of the things you can do with JavaScript:
- Add scrolling or changing messages to the browser's status line.
- Validate the contents of a form and make calculations. (For example, an order form can automatically display a running total as you enter item quantities.)
- Display messages to the user, either as part of a Web page or in alert boxes.
- Animate images or create images that change when you move the mouse over them.
- Create ad banners that interact with the user, rather than simply displaying a graphic.
- Detect the browser in use and display different content for different browsers.
- Detect installed plug-ins and notify the user if a plug-in is required.
You can do all this and more with JavaScript, including creating entire applications. We'll explore the uses of JavaScript throughout this book.
How JavaScript Fits into a Web Page | Next Section

Account Sign In
View your cart