PAOLO's WEB PAGE TUTORIAL:
HTML The Notepad Way!
Advanced Level (page 3)
A Beginner's Guide by Paolo M. Brion
What's On
This Page:
Java Applets and Javascript Basics
Final Words
Java Applets and Javascript Basics:
Well Java isn't just coffee any more. My computer/internet dictionary
defines Java as "a programming language developed at Sun Microsystems in
the mid 1990s to enable networked computers to transmit computations to
each other, not just data". In other words java is used to make mini programs
called "applets" which are small enough to be downloaded in a reasonable
amount of time over the internet. This is good news for you, the web surfer,
because now there's a whole new interactive way to view the net. We all
use full blown programs everyday, your web browser is one of these, and
they're interactivity gives them utility and helps us to be more productive
and in some cases have more fun. However, these programs on our computers
take up huge amounts of space though, and if downloaded over the internet,
the files that make them up would take forever to get to us on a modem.
This is where java comes in. In their "streamlined-for-the-internet" nature,
they can bring us such things as chat rooms, video games, special effects,
form makers and submitters, interactive (not just animated) images, and
whatever else a programmer can think of . In theory you could run a computer
without a huge hard drive or a lot of saved files (for software) in this
manner. If you wanted to run a program such as a wordprocessor, you'd just
download a java applet and bingo there it is! No need for extra software.
No need for all the complexities of your current computer. Don't chuck
your old desktop out the window yet however, there are a lot of things
a regular computer can do that a java based one can't.
But we're getting away from the point..... In the context of this tutorial,
java can make your web pages sparkle! Did you see a cool fade in when you
first loaded up this page? If didn't your browser's java capabilities are
probably turned off or it wasn't java capable in the first place. If tou
did however you would have witnessed an example of java...... javascript
to be exact. Java on the web can come in two forms; either as java ".class"
files or as javascript which is written into a page like HTML.
Java .class files are actual compiled executable files which your browser
runs in the same way windows 95 would run any other program. These "applets"
are downloaded the same way embeded images are on web pages. Once on your
computer they download whatever extra graphic or sound files they need
to run. All this downloading causes some applets to take a while to startup.
The downloading and the program-like nature of .class files is also
probably why large computer networks disable them for security reasons.
Despite these facts .class files can offer amazing results. Since they
are compiled programs they are easy to use and customize.
Javascript is uncompiled java. This mean that when you look at the
HTML source code of a web page with javascript, you'll see what looks like
a program written in C or C++ (the programming languages for java are similarly
also called J and J+). In other words, depending on what the javascript
does, you'll see something that looks like really complicated HTML. Javascript
usually loads faster than a java class file and runs faster. Since its
program is written in text it's especially easy to copy and paste it using
notepad onto your own HTML files. You might not understand what your copying
but hey it looks pretty. In fact there are several sites on the net with
examples meant for copying and modifying to fit your needs.
Well that's the basic info on Java. If you want more detailed information
try visiting its creators at Sun Microsystems.
Using
Notepad to Embed Java Applets on a Page
As I said java .class files are easy to use. They are controlled simply
by specifying "parameters" which act like the sub commands on HTML tags
(commands). The easiest way I know to embed these .class files onto your
HTMLs is through notepad. These applets are almost always graphical and
thus you would lay them out on pages in the same way you would an image,
specifying height, width, alignment, centering and so forth. Geocities
uses a .class file for its "geoguide" navigational tool. Let's use it as
an example.
A .class file is embeded with the HTML command pairs <applet>
and </applet>. The basic (geoguide) <applet>
command with all its sub commands looks something like this:
<APPLET NAME="geoguide" CODEBASE="/classes/geoguide"
CODE="GeoGuide.class"
ARCHIVE="GeoGuide.zip" ALIGN=middle WIDTH=468
HEIGHT=60 HSPACE=0 VSPACE=0 ALT="This is a java applet">
In it's complete form you'd end the <applet> with an </applet>
so it would look like this:
<APPLET NAME="geoguide"
CODEBASE="/classes/geoguide"
CODE="GeoGuide.class"
ARCHIVE="GeoGuide.zip"
ALIGN=middle
WIDTH=468 HEIGHT=60
HSPACE=0 VSPACE=0>
</APPLET>
The sub commands (stuff after the word APPLET) break down like this:
NAME - gives the applet a name which helps
the browser from getting "confused" when there is more than one .class
file or if there are several .class files and/or javascript running on
a page; it's only necessary when there is more than one applet.
CODEBASE - tells the applet where to get
any additional files it needs to run its program; it is only needed when
the extra files are in a different folder.
CODE - specifies which .class file will
be embeded; similar to the src sub command used with image and sound files.
ARCHIVE - sometimes files for a .class
file are zipped to take up less space and download faster; this tells the
applet which .zip file to look in; it's only necessary if the .class file
has an associated .zip file.
ALIGN, WIDTH, HEIGHT, HSPACE, and VSPACE
- exactly like the image sub commands
which position and align them; all the image layout sub commands work with
the <applet> command; width and height
are the only ones really necessary.
ALT - displays the message in quotes when
you put the mouse over the java visual effect; this is purely optional.
Simple right? Almost like embeding an image file. There's one more
thing I need to mention. Some older browsers don't support Java .class
files and, as I said before, some large networks like college networks
disable .class files (but not necessarily javascript) for security reasons.
People viewing pages with .class files on computer terminals like these
see absolutely nothing! That is, unless you stick either a text message
or embed an image right before the </applet> end command. For example
the "geoguide" HTML specifies a GIF to be loaded up in place of the applet
if it is disabled. Take a look at the HTML (with the basic <applet>
sub commands only):
<APPLET CODEBASE="/classes/geoguide"
CODE="GeoGuide.class"
ARCHIVE="GeoGuide.zip"
WIDTH=468 HEIGHT=60>
<a href="/cgi-bin/geoguidencsa.map" target="_top">
<img ismap src="/cgi-bin/homestead/GeoGuideLite_image?bgcolor=White"
alt=""></a>
</APPLET>
See the section in green that starts with <a
href="...... and ends with alt=""</a>?
That part is added to embed an image which links to a file called geoguidencsa.map
(an image map). The browser only dispalys this image if the .class file
fails to show up. You can change this entire section to anything you want
including text. For example if you had wanted to display the message "This
browser has java .class files disabled" in boldface instead of the image,
you'd type this:
<APPLET CODEBASE="/classes/geoguide"
CODE="GeoGuide.class"
ARCHIVE="GeoGuide.zip"
WIDTH=468 HEIGHT=60>
<b>This browser has java .class files disabled</b>
</APPLET>
And that's the basic command for the geoguide!
Working with Java Applet Parameters
As I mentioned .class files offer easy customizabilty through the use
of parameters. These parameters are specific to each applet and are stuck
between the <applet> and </applet>.
Let's take a look ince again at the geoguide applet (embeded with the image
for non java browsers) with some parameters added in:
<APPLET CODEBASE="/classes/geoguide"
CODE="GeoGuide.class"
ARCHIVE="GeoGuide.zip"
WIDTH=468 HEIGHT=60>
<PARAM NAME="bgcolor" VALUE="ffffff">
<PARAM NAME="label" VALUE="My Links">
<PARAM NAME="link1" VALUE="http://www.oocities.org;Geocities">
<PARAM NAME="link2" VALUE="http://www.zdnet.com;ZDnet">
<PARAM NAME="link3" VALUE="link 3 URL;Description">
<PARAM NAME="link4" VALUE="link 4 URL;Description">
<a href="/cgi-bin/geoguidencsa.map"
target="_top">
<img ismap src="/cgi-bin/homestead/GeoGuideLite_image?bgcolor=Beige"
alt=""></a>
</APPLET>
The stuff in green are several types of parameters which you can set
up for GeoGuide.class. When you want to customize a parameter, you start
by using the <param> command. You then
specify the name of the parameter by adding "name="
to it. For example, if you wanted to specify a background color for the
geoguide you'd start with: <param name="bgcolor">.
Finally you'd specify the type of change with "value=".
For example the entire command to make the Geoguide's background color
white you'd type: <param name="bgcolor"
value="ffffff">
The "label" parameter in the above example specifies a tab for the
users own links. The other parameters set up the links themselves, in this
case four of them. For example link1 is named Geocities
and links to the URL http://www.oocities.org
and link2 is named ZDnet which links to http://www.zdnet.com.
The other two links are not filled in. For complete instructions on the
possible parameters for the geoguide try going to the geoguide
installation page. All .class files are customized in a similar fashion
although parameters vary with the file. Only the guy who compiled the applet
can tell you all the possible parameters.
Adding
(gulp) Javascript with Notepad
Now javascript is real programming so I can't go into extensive detail
about it. You'll have to buy a book on programming or take classes or something
to master it. I can tell you the basics about it however. Once again the
only way to really add javascript is through the notepad method (the text
based way). The beginning of javascript is indicated by the <script>
command and ended with </script>. The only
sub command for <script>
which I know of is "language="
which tells the browser what programming language it is using. For Javascript
it is set equal to, what else, "javascript"
like so:
<script language="javascript">
The real meat and potatoes of javascript programming
goes in between the <script>
and <script>.
Basic
Variables, Buttons, and Pop-ups
Wanna see a quick javascript type example? Here it is, the button!
Actually it's not technically really javascripting, it's actually a
form button. The javascript part comes in when I specify an action (or
a function) to occur when someone clicks on the button. Clicking
on it right now just makes it go in and the pop back out.
The most basic of javascripts, like many other programming computer
languages, deal with variables. The button is one way of summoning a command
which can obtain a variable. Let's see some examples of button action first.
Here's a one that when clicked displays a pop-up alert message. Go ahead
try it:
(the HTML for this is: <form><input type=button
value="alert" onclick="window.alert('This
is an example of an alert pop-up box');"><form>
)
Here's another button that asks you to confirm something, don't worry
if you hit O.K. or Cancel I set the buttons to do nothing:
(the HTML for this is: <form><input type=button
value="alert" onclick="window.confirm('Confirm?');"><form>
)
O.K., one more button to satiate your curiosity, this one is more complicated.
It asks you to input text which then appears in the status bar on the lower
left corner of your browser:
(the
HTML for this is: <script>function inputtext(){var
text = window.prompt("Enter some text.\n It
will apear in the satutus bar at the lower left corner of the browser\n
when you hit enter or click on OK","type
your text here");window.status=(text)?text:'Done';}</script><form><INPUT
type=button value="click to input some text"
onclick="inputtext()"><form> )
You can imagine the kind the possibilities that these buttons can add
to a page. To be honest though, the only button that really uses javascript
was the last one, since when pressed it activates a javascript function.
Let's start at the beginning first, to make a button we add the following
tags to our page HTML where we want the button to appear:
<form><input type=button value="click
me"></form>
This makes a button like this:
However it doesn't do anything yet (like the first button example labeled
"a javascript button"). You can change the button's label by replacing
the text in blue which says "click me" to whatever you want. In order to
assign an action to a button you would include "onclick=" in between the
<input> tag so it would look something like:
<input type="button" value="click
me" onclick=" ">
The action goes in between the quotes after onclick=.
There are many possible commands to make a button perform an "action".
For example setting onclick like this:
onclick="location.href='http://www.oocities.org'
"
This will make the button take you to the Geocities homepage.
In order to make an alert message pop-up you'd type:
onclick="window.alert('YOUR
MESSAGE GOES HERE');"
You'd replace the stuff in blue with what you wanted the alert to say.
I can't list all the other commands. Like I said get a good book which
can go into more detail about javascript. In the meantime here's a couple
more of them to play with (remember you can change the stuff in blue text
to your desire):
window.open('INSERT
A URL HERE'); - This one makes a new
browser window open up to the URL you specify.
window.close(); - This one closes your
browser window. Be careful with this one!
parent.frame1.location="YOUR
URL HERE"; - This one is used only
with frames. It changes the
page in the frame you specify (replace frame1 with the name of the frame
you want to target). If you type the line twice or more times, targeting
different frames each time like this:
parent.frame1.location="YOUR
URL HERE";parent.frame2.location="YOUR
SECOND URL HERE";
You can change multiple frames at the same time by clicking
on only one button!
There are also a confirm pop-up and a "prompt" pop-up. As you saw in
the earlier examples the prompt asks you to input text and a confirm asks
you to confirm something by either clicking OK or CANCEL. These are therefore
more interactive than the alert pop-up and thus javasccript truly comes
in. You see, since the action depends on what you choose (OK or CANCEL,
or whatever text you might type), variables are required. A variable
in programming is a symbol or a string of text (a word) which "varies"
depending on what you set its value to. When you set the variables value,
for example you choose to hit CANCEL rather than OK on a confirm pop-up,
the program checks this value and performs the corresponding action. The
variable serves as kind of a "space holder" for an anticipated input (OK
or CANCEL for example).
So how do you set up a variable? Well it's simple, the HTML source
code is just:
<script> var
variablename=value
</script>
The text in blue is the variable's name and can
be any word (no phrases) you want, but try to keep it short. The text in
red is its value.
In this instance the variable's name is set as
"variablename" and its value is permanently set to "value". If you want
the value to be "variable" as the name implies, you have to set up a way
to get input from someone. One way to do this is by setting the value equal
to a prompt like so:
<script> var variablename=window.prompt('Please
input some text','put
text here');</script>
When linked to a button this makes a prompt
appear (when the button is clicked) which says "Please input some text"
with the words "put text here" in the field where you'd type something.
Whatever is typed will be the value of the variable. One way to link it
to a button is by putting all the stuf in-between the <script>
and </script>
tags within the onclick= quotation marks like so:
<form><input type="button" value="click
me" onclick=" var variablename=window.prompt('Please
input some text','put
text here'); "></form>
Another way to link to a button is by creating a function which
the button would activate when clicked. Making a function is similar to
making a variable. The HTML source code to declare a function is:
<script> function
functionname(){the
action goes here}</script>
Like a variable, a function can be named any word, but unlike variables
they must always end with a () (a pair of parenthesis). The stuff in red
can be set to any action. For example we can stick in there window.alert('YOUR
TEXT HERE'); or location.href=
which we learned about earlier.
As an example, let's use the action with a variable
that uses a prompt to set it's value. The HTML for this would look like
this:
<script> function
functionname(){var
variablename=window.prompt('Please input some text','put text here');}</script>
[note: there's a difference between ) and } ]
Since you don't actually see the function but only its result, you
can stick this anywhere within the HTML as long as the tag for the button
which activates it comes after it. You can even put it in the head
section of an HTML page. We named this function functionname()
so in order to link it to a button we would refer a button to it using
onclick= like
so:
<form><input type="button" value="click
me" onclick="functionname()"></form>
Ok now we put all that together and we get this:
<script> function
functionname(){var
variablename=window.prompt('Please input some text','put text here');}</script>
<form><input type="button" value="click
me" onclick="functionname()"></form>
This is the result try it out:
One last javascript trick before we proceed. If you want a function
or action to be activated when a page loads up or unloads (by going to
another URL) simply add onLoad="functionname()"
or onUnload="functionname()"
into the <body> or <frameset>
tags as in the following examples.
In the <body> tag:
<body onLoad="functionname()">
or <body onUnload="functionname()">
or using both <body onLoad="functionname()"
onUnload="functionname()">
In the <frameset> tag on a frameset page:
<frameset onLoad="functionname()">
or <frameset onUnload="functionname()">
or using both: <frameset onLoad="functionname()"
onUnload="functionname()">
Just replace the text in blue with the
name of the desired function or just add one of those action commands like:
window.alert("YOUR TEXT
HERE")
If you put the alert pop-up command in a message will automatically
come up when websurfers visit your page.
Write
Your Own Simple Script
That last button with the prompt to input text was pretty cool huh!
However, it didn't matter what you typed in the text field, nothing special
happened. That was because we didn't set up different stuff for the function
to do depending on what you typed in. Suppose you wanted a special message
to pop if some typed "hello" in the text field. What if you wanted the
browser to jump to the 24 HOUR JAVASCRIPT site when someone typed javascript?
How would you do that you ask? Well here's the part where variables come
in real handy. Since something like a prompt can set a value for a variable,
you can anticipate different inputs and perform actions for particular
ones! This is the perfect opportunity to write your first javascript program.
Here's how to do it. First off, we'll use the script from the last button
and add to it. More specifically we'll append the function so it performs
special actions depending on what the variable value is (what is typed).
In order to do this we'll use the conditional statements if
and else. I'll do it step by step so
you can follow along on notepad:
STEP 1: Let's retype the last function example in the following
easy to read arrangement. Let's also name our function "myfirstfunction()"
and our variable "myvariable". Let's make
the message on the prompt say "Type something. If
you want to go to the geocities home page type geocities below.".
<script> function
myfunction(){
var myvariable=window.prompt('Type
something.\n If you want to go to the geocities home page type geocities
below.','put
text here');
}
</script>
(note: \n skips to the next line. In this case "Type
something." will appear on one line and the next sentence will appear
on the next line below it)
STEP 2: Now we add if and else
statements. Lets's put one statemant that gives an alert response if someone
types "hello", one that jumps to the geocities site if "geocities" is typed,
and finally one that gives an alert response no matter what is typed in.
<script> function myfunction(){
var myvariable=window.prompt('Type
something.\n If you want to go to the geocities home page type geocities
below.','put
text here');
if (myvariable == "hello"){alert("Hi
there, my you sure are friendly")}
else
if (myvariable == "geocities"){location.href="http://www.oocities.org"}
else
{alert("I can't
read your handwriting, press the button and type something again.")}
}
</script>
STEP 3: Finally we add the HTML tags for the button remembering
to set onclick to equal the function's name, myfunction().
<script> function myfunction(){
var myvariable=window.prompt('Type
something.\n If you want to go to the geocities home page type geocities
below.','put
text here');
if (myvariable == "hello"){alert("Hi
there, my you sure are friendly")}
else
if (myvariable == "geocities"){location.href="http://www.oocities.org"}
else
{alert("I can't
read your handwriting, press the button and type something again.")}
}
</script>
<form><input type="button" value="click
me" onclick="myfunction()"></form>
STEP 4: Add that whole thing into your page's HTML and give yourself
a good pat on the back. You've just written your first javascript. Good
Job! Let's try a working example of it; test it by typing hello, geocities,
or anything else. If you type geocities make sure you come back to this
page. There's still more to learn! Here's the button:
Copy
It!
Well I hope you're proud of yourself. The power of Java is now at your
disposal! Now as I said somewhere before, this tutorial on Java is very
basic. The more complex scripts use multiple functions and variables all
working together to create one java program. If you want to master Java
you'll have to learn some computer programming. However, there is a shortcut.
Copy other people's scripts! You should know by now that javascript is
indicated by <script> on a page's source
code. When you see a cool example of javascript view the page's source
and copy, copy, copy! Then cut and paste it into your own pages. Of course
you should be careful what you copy. Some scripts and java applets are
copyrighted so you must first get their authors permission and give them
acknowledgment. There are some sites that offer tons of examples of javascript
for you to copy royalty free. 24 HOUR
JAVASCRIPTS and WEB
REFERENCE's JAVASCRIPT TIP OF THE WEEK are good places to browse for
scripts. Check out the java section on my Smart
Linx page for some more quality links. Once you have the copied scripts
just edit them to suit your needs. You should have at least some idea of
what a few commands do from what we covered in this tutorial. Luckily most
javascripts commands have logical names so with enough experience you can
gain some intuition on how they work which helps when editing copied programs.
Good luck!
Putting it All Together: A Final Word on Web Page Authoring
| tutorial page1 | tutorial
page2 | tutorial page3 |
| GEOCITIES | Home
Page |