Using Variables and Built-in Functions to Update Your Web Pages Automatically | WebReference

Using Variables and Built-in Functions to Update Your Web Pages Automatically


[next]

Using Variables and Built-in Functions to Update Your Web Pages Automatically

By thau!

With JavaScript you can update the content on your pages automatically—every day, every hour, or every second. In this chapter, I'll focus on a simple script that automatically changes the date on your web page.

Along the way you'll learn:

  • How JavaScript uses variables to remember simple items such as names and numbers
  • How JavaScript keeps track of more complicated items such as dates
  • How to use JavaScript functions to write information to your web page

Before getting into the nuts and bolts of functions and variables, let's take a look at a couple of examples of web pages that automatically update themselves, starting with the European Space Agency. As you can see in Figure 2-1, the ESA's home page shows you the current date. Rather than change the home page every day, the ESA uses JavaScript to change the date automatically.

Figure 2-1: Using JavaScript to display the current date

An even more frequently updated page is the home page of the Book of JavaScript website, which updates the time as well as the date (see Figure 2-2). You don't have to sit in front of your computer, updating the dates and times on your websites. JavaScript can set you free! The ability to write HTML to web pages dynamically is one of JavaScript's most powerful features.

Figure 2-2: Dynamically updating the date and time

To understand how to update the date and time on the page, you'll first have to learn about variables, strings, and functions. Your homework assignment at the end of this chapter will be to figure out how to add seconds to the time.



[next]

URL: