This reading is not a complete JavaScript tutorial. It is designed to help you embed pre-written JavaScripts into your page. It provides some, but limited explanation on what JavaScript is and how you can write it yourself. For those who want to (optionally) learn more, there are links to full tutorials at the end of this one.
Scripts can be put in either of the following two places in an HTML document.
Try opening a text editor (like notepad) and typing in the following code.
<html>
<head>
<title>My First JavaScript</title>
</head>
<body>
<script language = "JavaScript">
document.write( "Welcome to JavaScript!");
</script>
<br>
I'm outside the script
now.
</body>
</html>
Try saving your file as an html file and opening it in a web browser.
Your screen should look as follows:
An object is a data type in JavaScript. An object has
properties (attributes) and methods (actions the object can do).
For example you can think of car as an object.
Some of
the properties associated with a car object could be:
Some of the methods associated with a car object could
be:
This is our introduction to the document object. The
document object represents the document
(in this case
an HTML document) displayed in the window.
The document object has properties that specify information about the (HTML) document displayed in the browser. The document object also has methods that allow JavaScript programs to dynamically output text into a document while the document is being parsed.
In the statement above we have used the write()
Method.
To use the write() Method we must
use the (dot) . operator. We simply connect the object and the desired
method with a dot:
document.write( )
The write() Method generally takes a string as an argument. If the argument is not a string, it will be converted to a string.
In this example the argument of the write() Method is
the string: "Welcome to JavaScript!"
which is
consequently displayed in the document in the browser window. The text "I'm
outside the script now" is not an argument to the write() Method, it is
just text typed into the body of the HTML file.
The write() Method can take more than one argument. For
example you could have a statement as follows:
document.write ("Welcome to my webpage " , username ," !");
Note that the statement is ended with a semicolon.
That is all that we are going to say about background of JavaScript, although there is a lot more. Instead, look at the following examples and their explanation. Go through each example.
After going through the above examples (mandatory), you can (optionally) look at these sources for more information:
There are many books on JavaScript available at places like Amazon, Barnes&Noble, etc. Learning JavaScript is important for doing web page construction and is a good introduction to general computer programming.