- Learning Web Scripting Basics
- How JavaScript Fits into a Web Page
- Exploring JavaScript's Capabilities
- Displaying Time with JavaScript
- Testing the Script
- Summary
- Q&A
- Workshop
Exploring JavaScript’s Capabilities
If you’ve spent any time browsing the Web, you’ve undoubtedly seen lots of examples of JavaScript in action. Here are some brief descriptions of typical applications for JavaScript, all of which you’ll explore further, later in this book.
Improving Navigation
Some of the most common uses of JavaScript are in navigation systems for websites. You can use JavaScript to create a navigation tool—for example, a drop-down menu to select the next page to read, or a submenu that pops up when you hover over a navigation link.
When it’s done right, this kind of JavaScript interactivity can make a site easier to use, while still remaining usable for browsers that don’t support JavaScript (or HTML5/CSS3, which can also be used to create great navigation).
Validating Forms
Form validation is another common use of JavaScript, although the form validation features of HTML5 have stolen a lot of JavaScript’s thunder here as well. A simple script can read values the user types into a form and make sure they’re in the right format, such as with ZIP Codes, phone numbers, and e-mail addresses. This type of client-side validation enables users to fix common errors without waiting for a response from the web server telling them that their form submission was invalid. You’ll learn how to work with form data in Chapter 26, “Working with Web-Based Forms.”
Special Effects
One of the earliest and most annoying uses of JavaScript was to create attention-getting special effects—for example, scrolling a message in the browser’s status line or flashing the background color of a page.
These techniques have fortunately fallen out of style, but thanks to the W3C DOM and the latest browsers, some more impressive effects are possible with JavaScript—for example, creating objects that can be dragged and dropped on a page, or creating fading transitions between images in a slideshow. Some developers have HTML5, CSS3, and JavaScript working in tandem to create fully functioning interactive games.
Remote Scripting (AJAX)
For a long time, the biggest limitation of JavaScript was that there was no way for it to communicate with a web server. For example, you could use JavaScript to verify that a phone number had the right number of digits, but not to look up the user’s location in a database based on the number.
Now that some of JavaScript’s advanced features are supported by most browsers, this is no longer the case. Your scripts can get data from a server without loading a page, or send data back to be saved. These features are collectively known as AJAX (Asynchronous JavaScript and XML), or remote scripting. You’ll learn how to develop AJAX scripts in Chapter 25, “AJAX: Remote Scripting.”
You’ve seen AJAX in action if you’ve used Google’s Gmail mail application, Facebook, or any online news site that allows you to comment on stories, vote for favorites, or participate in a poll. All of these use remote scripting to present you with a responsive user interface that works with a server in the background.