Where do your JavaScript codes go? Well, basically
anywhere inside the <html> tags of your page. The beginning
of your code begins with <script> and ends with </script>
<html>
<head><title>This is an example page</title></head>
<body>
Welcome to the JavaScript course!
<script
language="JavaScript">
<!--
document.write("Hi there. This text is written using
JavaScript!")
//-->
</script>
</body>
</html>
Output:
Hi
there. This text is written using JavaScript!
As you can see, we began our script with the
tag <script language="JavaScript">
The part in orange is purely optional, but you should include
them to remind yourself-and others that you are using JavaScript
now. The second and next to last lines of the above example
are <!-- and //-->,
which are the comment tags. These tags should ALWAYS be included
to help hide your code against older browsers of both Netscape
and IE. If you don't include them, and someone is using an
old browser, the browser will just "dump" all your
code as text onto the screen, in other words, not a pretty
sight! The only "functional part" of this script
is the document.write(".......")
part. It basically writes to
the page whatever you put inside the quotation marks. Don't
worry so much about why this is so yet, we will discuss this
in detail later. We end this entire code with </script>
This terminates your script, and brings you back to html.
Like html, you can insert
comments in your JavaScript codes. Comments are ignored by
the browser, and only used as reminder or documentation for
your code. To basic syntax of inserting comments is either:
//
for single-lined comments,
or
/*
.......*/
for multiple ones.
For example:
<script
language="JavaScript">
<!--
//this script does nothing and is useless!
/*Hay, don't involve me
in this!*/
//-->
</script>
Ok, we are now ready to
proceed to some real programming!
JavaScript, like many
programming languages, relies on objects, functions, and event
handlers to create workable programs. If you know absolutely
nothing about these technical terms and how to use
them, don't worry. By the time we're through with this tutorial,
you will.
|