Chapter 2

Know Your JavaScript

IN THIS CHAPTER

  • Understanding JavaScript properties and methods
  • Storing your data in variables and arrays
  • Making decisions with the if statement
  • Looping with the for and while loops
  • Connecting JavaScript to HTML buttons

Ajax is based on JavaScript, and this chapter is all about JavaScript. You're going to need a good foundation in JavaScript to tackle Ajax, and all the JavaScript you'll need to know is in this chapter. If you're already up to speed in JavaScript, you can skip this chapter and turn to Chapter 3, which gets into Ajax in depth.

What Is JavaScript's Place in Ajax?

Where does JavaScript belong in the scheme of things? It turns out that it's absolutely central; JavaScript is what makes Ajax work. To see that, take a look at an example.

This example downloads and displays text from a server. (You'll see what makes it tick in detail in the next chapter.) You can see this example in a Web browser in Figure 2.1.

FIGURE 2.1 An Ajax demo

images

When this application is hosted on a Web server, clicking the Fetch the Message button causes the application to use Ajax techniques behind the scenes to connect to the server and download the contents of a text file, data.txt. Here's what's in data.txt:

This text was fetched from the server with Ajax.

Clicking the button downloads the message from the server, without any page refresh needed. The downloaded ...

Get Ajax Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.