In this lesson, you will learn how to embed your JavaScript code in HTML documents, and so use the browser itself to execute your code. While the JavaScript interpreter that you have been using will still be useful for designing and testing pieces of code, the ability to integrate code with other HTML elements will open up a world of new possibilities.
Throughout this course, you have been writing and executing your JavaScript code in a simple interpreter environment. The JavaScript interpreter that you have been using was written specifically for this course, allowing you to learn programming with minimal overhead. The interpreter page is divided into two frames, one containing a text box for entering your code and a separate frame for displaying the output of your code. When you click on the Execute button in the input frame, the interpreter takes your code, embeds it in the appropriate HTML code, and then loads that code into the output frame. The actual execution of your code is performed by the Web browser itself.
Now that you know a little about how Web pages work, the interpreter page no longer is necessary (although it can still be useful). Instead, you can embed your JavaScript code directly into HTML documents and execute that code by loading the page into the browser. Not surprisingly, you must identify JavaScript code in the HTML document using tags. Any code surrounded by the tags <SCRIPT LANGUAGE="JavaScript"> and </SCRIPT> will be executed by the browser, and the resulting output of that execution will be displayed on the page.
For example, consider the following HTML document.
|
|
This page contains a single line of JavaScript code, which displays the message "Hello World Wide Web!". When this page is loaded into the browser, the SCRIPT tags identify code that is to be executed. The output of the code, in this case the single message, will appear on the Web page.
While this example is simple, it is not very compelling. In fact, you could easily achieve the same effect by entering this message as regular HTML text. Suppose, however, that you wanted to display the message some number of times in the page. It would be tedious to have to enter multiple copies of the message, and it would also be cumbersome if you decided to change the number of repetitions. On the other hand, JavaScript makes this task trivial:
|
|
Write the HTML code for such a page and save it in the document psychic.html. Load the page to execute your code and verify that it does indeed display a different "lucky" number every time. Feel free to elaborate.
An important step in building abstractions for problem solving is library use. Useful routines can be encapsulated in a library file and then loaded whenever they are needed. The use of libraries simplifies programming and allows you to think at a higher level of abstraction.
The JavaScript interpreter had a special box in which you entered the names of any library files that were to be included. This same effect can be achieved in HTML by specifying the SRC attribute in the SCRIPT tag. For example, the following element, when included in an HTML document, will load the JavaScript library named random.js.
With the exception of Turtle Graphics (which were specially handled by the interpreter), any JavaScript code that you wrote in the interpreter can be written directly in an HTML document. This includes function definitions. As was the case for loading libraries, function definitions should be defined in the HEAD section of the page to ensure that they are properly loaded before attempting to call them.
For example, the following HTML document contains the definition of the diceRoll function, which uses the randomInt function from random.js.
|
Wasn't that exciting ? |
You should note that the JavaScript code in the BODY is mixed in with regular HTML text. This is certainly common, since most text in a Web page can be written directly without the use of JavaScript.
Exercise 6:
A more interesting application involving dice is the casino game craps. Craps is a relatively easy game to understand:
Using pseudo-code, the algorithm for playing the game of craps can be described as follows: