Listing 4.1 A Simple HTML Document with a Simple Script
<!doctype html> <htmllang="en"> <head> <meta charset="utf-8"> <title>The American Eggplant Society</title> </head> <body> <h1>The American Eggplant Society</h1> <p>Welcome to our site. Unfortunately, it is still under construction.</p> <p>We last worked on it on this date: <script> <!-- // Hide the script from old browsers document.write(document.lastModified); // Stop hiding the script --> </script> </p> </body> </html>
Figure 4.1 Using document.write to display a last-modified date.
In this example, we placed the script within the body of the HTML document. There are actually four places where you might use scripts:
In the body of the page—In this case, the script’s output is displayed as part of the HTML document when the browser loads the page.
External scripts are supported by all modern browsers. To use an external script, you specify its filename in the <script> tag, as shown here:
Listing 4.2 A Simple Event Handler
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>Event Test</title> </head> <body> <h1>Event Test</h1> <button type="button" onclick="alert('You clicked the button.')"> Click Me!</button> </body> </html>