1.1.1 What Is an HTML Document?
HTML is an acronym for H yper T ext M arkup L anguage. HTML documents , the foundation of all content appearing on the World Wide Web (WWW) , consist of two essential parts: information content and a set of instructions that tells your computer how to display that content. The instructionsthe markup, in editorial jargoncomprise the HTML language. It is not a programming language in the traditional sense, but rather a set of instructions about how to display content. The computer application that translates this description is called a Web browser . Ideally, online content should look the same regardless of the operating system on which a Web browser resides or the browser used. This goal of complete platform independence is achieved only approximately in practice.
These elements define the essential parts of an HTML document: the document itself, a heading section, a title section, and a body. Each of the elements is defined by two tags a start tag and an end tag. Tags are always enclosed in angle brackets: <> . End tags start with a slash ( / ). As will be shown later, some HTML elements have only one tag. Most tags are supposed to occur in pairs, although this rule is enforced only loosely in HTML. In order to support a scripting language such as JavaScript (much more about that later!), another element must be added to the four basic elements:
As used in this book, a script element always contains JavaScript code.
These elements are organized as follows within an HTML document:
The html tag encloses all other tags and defines the boundaries of the HTML document. We will return to the other tags later. script tags often appear inside thetag, but they can appear elsewhere in a document, too. The indenting used to set off pairs of tags is optional, but it makes documents easier to create, read, and edit. This style is part of good programming practice in all languages.
Because JavaScript is so tightly bound to HTML documents, you must learn JavaScript along with at least a subset of HTML. Unfortunately for anyone trying to learn and use HTML and JavaScript, each of the several available browsers is free to implement and support JavaScript in its own way. A browser doesnt even have to support JavaScript at all, although it is hard to imagine why it wouldnt. Browsers can and do incorporate some proprietary HTML and JavaScript features that may not be supported by other browsers. Newer versions of any browser may support features that wont be recognized by earlier versions.
Fortunately, it is possible to work with what is essentially a de facto standardized subset of HTML and JavaScript. As a result, some of the descriptions of the details of HTML and JavaScript in this book will be incomplete; this is not necessarily a bad thing!
Although HTML documents are usually considered to be a way of distributing information for remote access on the Web, they are equally useful when used locally on any computer that has a browser. So, in conjunction with JavaScript (and later with PHP), you can create a self-contained problem-solving environment that can be used locally as well as (literally) globally.
Good programming technique often involves separating the input/output (I/O) interface from the underlying calculations that do the work of a program. The programming environment provided by HTML/JavaScript provides a conceptually elegant means of implementing this strategy. An HTML document provides the I/O interface and JavaScript (and/or PHP, as will be seen later in this book) handle the calculations. An advantage of HTML is that it provides a wealth of interface possibilities that far surpass those of text-based languages such as C.
1.1.2 What Is JavaScript?
JavaScript is an interpreted (rather than compiled ) object-oriented programming language that has been developed for use alongside other Web tools. JavaScript does not operate as a standalone language. It is designed to work together with HTML for creating interactive Web pages. It is not the same as Java, which is a compiled object-oriented language.
JavaScript is used to write client side applications , which means that JavaScript code is sent to a users computer when a Web page is loaded. The code is then executed, basically line by line, by a JavaScript interpreter included as part of the users (clients) Web browser. This arrangement minimizes security issues that can arise when a client computer interacts with the computer that sent the page. It also makes it easy to package an entire problem, with its own user interface and solution, self-contained within a single document. But the inability to interact dynamically with information stored on a server imposes limitations on the kinds of tasks that JavaScript can accomplish.
It is commonplace to refer to any set of written computer instructions as a program. However, this term is more rigorously applied to a separate entity that can be executed on its own. Because JavaScript is interpreted rather than compiled, a separately executable entity is never created. Instead, JavaScript code statements are interpreted and executed one at a time, essentially on the fly. Although this may seem inefficient, there is rarely any discernible time lag associated with executing JavaScript commands on modern computers.
JavaScript is one of a class of scripting languages whose purpose is to access and modify components of an existing information interface. (Microsofts VBScript is another scripting language.) In this case, the interface is an HTML document. As soon as HTML documents on the Web evolved from one-way delivery systems for displaying fixed content, something like JavaScript immediately became necessary. One of its first applications arose from the need to check values entered by users into the fields of HTML forms that can be sent back to the originator. (Forms are discussed in a later chapter.) JavaScript can be used to compare input values against an expected range or set of values and to generate appropriate messages and other actions based on those comparisons.
JavaScript has evolved into a complete programming language with extensive capabilities for manipulating text and handling mathematical operations, useful for a wide range of computing problems. Possible applications include many self-contained scientific and engineering calculations. As noted earlier, JavaScript is restricted to problems that do not need to access external data sources, regardless of whether those sources reside on a local computer or on a remote server.
As previously noted, the major challenge in learning HTML/JavaScript is that it is not a completely standardized environment. The various dialects of HTML and JavaScript pose problems even for experienced programmers. These kinds of problems can be minimized by focusing on an appropriate subset of HTML/JavaScript. This is feasible because there is little reason to use browser-specific subsets of HTML/JavaScript in the context of the topics dealt with in this book.