Javascript Tutorial
Written by Colin D. Joye 1/00
Javascript really is not a very well built language, it's buggy and can even be dangerous to use
(I've crashed Netscape many times using it), but it's easy to learn, very simple, very effective and you don't need a compiler, like Java and other
more complicated (but far more powerful) languages.
Javascripts can be inserted almost anywhere in an HTML document: inside the <head> or inside the
<body> are the standard places (not inside a table or stylesheet). The head is the recommended place
for most scripts since the head loads first and it won't seem like your images are loading slow, but
that the server is slow.
The <script> and </script> tags are used to define the area where the javascript
is written. See "Basic Structure" below.
An incomplete listing of the possible functions that I know of: (click on the linked
functions for an example)
var x; --- declares variable "x". I have seen code without variable declarations and
without semicolons at the end of a line, but I don't know under what conditons that is acceptable.
var hex = new Array(8,9,"a","b",...); --- creates array called "hex" which is
initialized to "8,9,a,b" and can be used by indices: "hex[2]" in this case is "a".
function f(arg1,arg2...){"code"} --- declares a function, "f" taking two arguments,
"arg1" and "arg2". "code" can be javascript code, comments, or other functions.
-
window.open('file.html'); --- opens a new broswer window
-
window.close(); --- closes current broswer window
-
window.location; --- gets or defines browser "location" (URL).
-
window.document.write("code "); --- writes e.g. HTML code to a new page (use
"alt + <-" or browser button to return).
-
window.status='string'; --- put text at the bottom of the screen where the links locations
are viewed.
-
document.layers[n].left=x; --- places layer "i" at "x"-pixels position horizontal
(link puts on orange square at [x=100px,y=570px] next to the words "Javascript Tutorial").
Basic Structure:
document.layers[n].top=y; --- places layer "i" at "y"-pixels position vertical
-
Math.sin(); --- sine function (radians)
Math.cos(); --- cosine function (radians)
Math.tan(); --- tangent function (radians)
-
Math.random(); --- random number generator (output: 0 ... 1)
-
alert("msg "); --- displays "msg" in an alert pop-up.
-
onMouseover=" stuff "; --- executes "stuff" when pointer touches object (e.g. link)
-
onMouseout=" stuff "; --- executes "stuff" when pointer leaves object (e.g. link)
-
onClick=" stuff "; --- executes "stuff" when clicked on.
-
Some sample codes which do useful things:
onLoad="window.location='default.htm';" --- A automatically "redirects" the browser
to "default.htm" when placed in the <body> tag of an Html document. very useful in my case where
I have a shorter URL for my pages on one server http://ee.vill.edu/~cdanjo/,
but have all my stuff on a different server, http://students.engineering.villanova.edu/cjoye01.
-
.(); ---
|