HTML and CSS Reference
In-Depth Information
The window portion of the code identifies the browser
object resizeTo() is an
applied function . The values in the parentheses are called arguments or parameters . All JavaScript
statements must end with a semicolon.
object to be affected, and
When it comes to web pages, JavaScript works by identifying page elements and modifying them or
by inserting new elements. You can use JavaScript, for example, to dynamically change text on one
part of the web page when the user hovers over another part of the page. Another common use of
JavaScript is to display an alert box — a new element — when an error is encountered. To identify
the various page elements, JavaScript uses the document object model or DOM .
The DOM is, essentially, a road map to any given web page. JavaScript uses the DOM to pinpoint
a precise page element — such as a text field in a specific form — and analyze, modify, or delete its
content. The DOM is made up of a series of objects including window , document , history , form ,
text , and others. To identify an element, JavaScript uses what's known as dot notation to drill
down through the various objects.
For example, say you have a text form control with a name of fullName that is contained in a form
named myForm . To find the current value of that field — what's in the text box, in other words —
your JavaScript might look like this:
var theEntry = window.document.myForm.fullName.value;
Note the periods that separate each object: These are the “dots” in dot notation. Because JavaScript
is a scripted language, much of JavaScript's work is done with simple equations like this where you
can get or set the current value of a page element. The code var is short for variable and, unlike in
many other computer languages, you don't need to specify whether the variable is text, number, or
some other type.
JavaScript is a case-sensitive language, so you need to be careful about naming
variables and functions exactly. In other words, theEntry is not the same as
TheEntry or theentry .
Now that you have captured what was entered in the fullName text field, you can use that in
another common JavaScript function, alert() , which displays a message box:
alert(“Thanks for entering, “ + theEntry);
The plus sign is used here to bring together — or concatenate — the static text string ( “Thanks for
entering, “ ) and the variable, theEntry . When incorporated into a page that I had visited, the
resulting message box might look like the one in Figure 21-1.
Search WWH ::




Custom Search