What is JavaScript?
Dateline: 09/11/99
Dynamic! Interactive!
I keep hearing these buzzwords, but what do they mean? What do I need
to make my Web site "dynamic" and "interactive?"
The answer to this question and many more is "JavaScript." So
just what is JavaScript?
JavaScript is an extension of HTML that allows the programmer to do things
with the Web page as it is displayed to the user. Where HTML is the
skeleton that gives a document its appearance and structure, JavaScript is the
brains and the muscles that give a page life.
JavaScript is not Java. Java is full-fledged, compiled (sort-of)
application development language. Java makes applications that can
function on their own (sort of like Microsoft Word) or mini-applications called
"applets" that can run inside of a web browser. If you're
looking for Java, visit About.com's Java Web
site.
Why does JavaScript sound like Java? JavaScript got the
"Java" in its name because its syntax is based on Java. In other
words, most statements in both languages are written in a similar
manner.
Where does JavaScript go? As I said before, JavaScript is an extension
to HTML. This means that the JavaScript code goes right in with the HTML
code. JavaScript code goes between the <script> tag, like so:
<script>
... JavaScript code goes here ...
</script>
The <script> tag has a couple of different options, but most important
is the "language" option. Since some browsers can interpret
different types of script code (such as VBScript), you should always specify
language="JavaScript" when you are using JavaScript.
<script language="JavaScript">
... code ...
</script>
Another trick to note is that older browsers (Netscape 1.X, Internet Explorer
2.X) do not support JavaScript and do not recognize the <script>
tag. These older browsers will ignore the <script> tag and simply
display the JavaScript code as if it were regular text on the page. To
avoid this, surround your JavaScript code with comment tags like so:
<script language="JavaScript"><!--
... code ...
// --></script>
This way, if the browser doesn't recognize the <script> tag, it will
ignore everything between the <!-- and the --> tags. Browsers that
do recognize the <script> tag are aware of this little trick, and they
will ignore the <!-- and the --> tags, but will still interpret the
JavaScript code. (Note: the // in front of --> is very important.
Do not leave this out.)
JavaScript makes web pages dynamic and interactive primary through something
called the Document Object Model (DOM). The DOM is a way of simplifying
the structure of an HTML document so that it can be easily understood and
accessed by mere mortals.
The DOM represents the HTML document as "objects." It's
difficult to describe an object as anything other than an object: an object is
just a thing that has properties and can contain other objects. As an
example, there is an object called "document," that represents the
HTML document. If your HTML looks something like this:
<html>
<body>
<form name="myForm">
<input type="text" name="myText">
</form>
</body>
</html>
Then the "document" object would contain an object called "myForm."
You can access the "myForm" object by using "document.myForm."
"myForm" contains an object called "myText," which can be
accessed using "document.myForm.myText." If we wanted to set the
value of the "myText" textbox to say "hello," we would use
the following JavaScript code:
document.myForm.myText.value = "hello";
Through this method, we can modify nearly every aspect of our HTML document
on the fly. To find out some of JavaScript's uses, visit Uses
for JavaScript. For some beginner's tutorials, visit Getting
Started.
Visit the message boards!
Previous Features
|