An Introduction to JavaScript
by OutFront moderator JohnT
Welcome to the first in a series of lessons in
JavaScript from Outfront Moderator John T. This series
of lessons is meant to teach JavaScript to the
beginner.
We have all seen JavaScript in action while surfing the
web. The image flips (known as image rollovers)
on web pages are done with JavaScript. Also,
things like alert messages, date and time displays,
pop-up windows, form confirmation pages, scrolling
text, browser detection and many other functions are
performed with JavaScript. A recent study
indicates that nearly 90 percent of the top 500
websites (based on traffic) use JavaScript.
JavaScript enables the web developer to add dynamic
functionality to a web page. JavaScript is a
powerful scripting language that is also capable of
performing extensive form data collecting and form
processing functions.
There is a major difference between JavaScript and
Java. JavaScript may be directly incorporated into
web page HTML, and
runs through a browser. Java cannot be written
directly into HTML. Java is a more complex
programming language where code is first written, then
put through a compiler. JavaScript was developed
by Netscape, whereas Java was developed by Sun
Microsystems. The languages share some
similarities, but are quite different.
JavaScript is an object-orientated scripting language.
This means that actions are performed on objects such
as windows and documents. We refer to a method as
performing an action on an object. Therefore in
JavaScript, we write object.method, in that order.
As you will see during our lesson series, the ordering
of the elements in JavaScript is very important.
JavaScript is a very logical scripting language.
I believe that it is this logical progression that
makes JavaScript easy to learn.
Well, let’s begin with our first JavaScript. We
will start with a JavaScript that simply writes some
text to a page. Let’s construct a script that
writes Learning JavaScript with John T is easy and
interesting to a web page. The script is given
below:
<script
language=”JavaScript”>
document.write(“Learning JavaScript with John T is
easy and interesting”);
</script>
Let’s examine our script in detail. We need to
first tell the browser that a JavaScript is about to
follow. So we do this by the
<script language=”JavaScript”> command.
We told the browser that the script was ending by using
the </script> command.
All right, then it stands to reason that the
document.write command writes the text to the page.
Document is the object,
and write is the method being performed on the object.
The text that is included in quotations within the
parentheses is written to the page. Text in a
document.write command must always be in parentheses.
We also need to tell the browser that the line of
JavaScript is ending. We do that by adding a semicolon
at the end of the line.
Try it out for yourself. Since most of you are
FrontPage users, open up a new page in FrontPage, go to
the HTML tab, and drop the script in between the
<head> </head> tags. Then view the
page with the Internet Explorer or Netscape browser to
see the script in action.
JavaScript is often placed in between the <head>
and </head>, but not always. As we will see
in future lessons, placement varies based on the type
of script and the script’s function. In our
next lesson we will also discuss the comment tags that
you have most likely seen before (a very popular
comment tag that starts with <!--).
Well, that is all for our first lesson and brief
introduction to JavaScript. I look forward to our
next lesson.