|
Free Open Book
Sams Teach Yourself JavaScript in 24 Hours |
How JavaScript Fits into a Web PageAs you hopefully already know, HTML is the language you use to create web documents. To refresh your memory, Listing 1.1 shows a short but sadly typical web document. Listing 1.1. A Simple HTML Document
This document consists of a header within the <head> tags and the body of the page within the <body> tags. To add JavaScript to a page, you'll use a similar tag: <script>. The <script> tag tag>>tells the browser to start treating the text as a script, and the closing </script> tag tells the browser to return to HTML mode. In most cases, you can't use JavaScript statements in an HTML document except within <script> tags. The exception is event handlers, described later in this hour. JavaScript and HTMLUsing the <script> tag>>tag, you can add a short script (in this case, just one line) to a web document, as shown in Listing 1.2. Did you Know? If you want to try this example in a browser but don't want to type it, the HTML document is available on this book's website (as are all of the other listings). Listing 1.2. A Simple HTML Document with a Simple Script
JavaScript's document.write statement, which you'll learn more about later, sends output as part of the web document. In this case, it displays the modification date of the document. By the Way Notice that the <script> tag in Listing 1.2 includes the parameter type="text/javascript". This specifies the scripting language to the browser. You can also specify a JavaScript version, as you'll learn later in this hour. In this example, we placed the script within the body of the HTML document. There are actually four different places where you might use scripts:
Using Separate JavaScript FilesWhen you create more complicated scripts, you'll quickly find your HTML documents become large and confusing. To avoid this, you can use one or more external JavaScript files. These are files with the .js extension that contain JavaScript statements. External scripts are supported by all modern browsers. To use an external script, you specify its filename in the <script> tag: <script language="JavaScript" type="text/javascript" src="filename.js"> </script> Because you'll be placing the JavaScript statements in a separate file, you don't need anything between the opening and closing <script> tagsin fact, anything between them will be ignored by the browser. You can create the .js file using a text editor. It should contain one or more JavaScript commands, and only JavaScriptdon't include <script> tags, other HTML tags, or HTML comments. Save the .js file in the same directory as the HTML documents that refer to it. See the Try It Yourself section of Hour 2 for an example of separate HTML and script files. Did you Know? External JavaScript files have a distinct advantage: You can link to the same .js file from two or more HTML documents. Because the browser stores this file in its cache, this can reduce the time it takes your web pages to display. EventsMany of the useful things you can do with JavaScript involve interacting with the user, and that means responding to eventsfor example, a link or a button being clicked. You can define event handlers within HTML tags to tell the browser how to respond to an event. For example, Listing 1.3 defines a button that displays a message when clicked. Listing 1.3. A Simple Event Handler
In Hour 9, "Responding to Events," you'll learn more about JavaScript's event model and creating simple and complex event handlers. By the Way You can also use an external script to define event handlers. This is a good practice because it lets you keep all of your JavaScript in one place, rather than scattered across the HTML document. See Hour 9 for details. |
Main Menu |
| 500 Juegos Gratis | 500 Giochi Gratis | 500 Jeux Gratuits | 500 Jogos Gratis | 500 Kostenlose Spiele |