JavaScript is cross-platform, object-oriented scripting language, which
was developed by Netscape. With JavaScript you can easily create interactive
web-pages.
JavaScript is not Java! Although it has a similar name, it's totally
different language!
There are 3 types of JavaScript:
Core JavaScript - It's the common part for all JavaScript
implementations. Core JavaScript contains a core set of objects, such as
Array, Date, and Math, and a core set of language elements such as
operators, control structures, and statements.
Client-Side JavaScript - This is the JavaScript, which is
working in the browser. It includes core JavaScript and extentions to use
within the browser (like window object etc.). This type of JavaScript
supported by most of the modern browsers.
Server-Side JavaScript - This is the JavaScript which runs
on the server, which supports it (Netscape LiveWire). It includes core
JavaScript and server extentions. It's supported by Netscape server. For more
info on this see Netscape LiveWire
part of this course.
We'll talk about the Client-Side JavaScript. The way you add JavaScript to the
HTML page is inside:
<SCRIPT LANGUAGE=JavaScript>... </SCRIPT> .
Or you can import the external file with JavaScript:
<SCRIPT LANGUAGE="JavaScript" SRC="filename.js"></SCRIPT> .
Here is a simple example:
I don't want to describe here the syntax of JavaScript, you can find it in
any JavaScript tutorial (see references
or learn from examples.
Some examples of the standard objects in Client-Side Javascript:
Note, that it's far from being a full reference, please consult
Netscape Client-Side JavaScript Reference for the full list of available
methods and objects.
Core JavaScript:
Array - Provides the functionality to work with arrays
(have a properties like length, methods like sort,
reverse, concat etc.)
Date - Lets you work with dates and times.
Math - Has properties and methods for mathematical
constants and functions. For example have a properties like E, PI
and methods like abs, log, sin etc.
Client-Side JavaScript:
Button - Allows to work with buttons on an HTML form.
Properties like form, name, value and methods like click,
focus etc.
document - Contains information about the current
document, and provides methods for displaying HTML output to the user.
Properties like forms - array of forms on this document (objects),
cookie, images - array of images etc. You can do all kinds
of things with this object, e.g. if you want to change the first image
on your document from the empty_cart.gif to full_car.gif:
document.images[0].src = "full_car.gif";
Form - Lets you work with forms in the document.
Properties like action, elements - array with all form
elements (buttons, checkboxes, text fields etc.). Methods like
submit - submits the form, reset etc. Example: if you need
to submit the form from your function, just do form.submit()
History - Contains an array of information on the URLs
that the client has visited within a window. This information is stored in
a history list and is accessible through the browser's Go menu. Properties
like current, previous, methods like back, forward.
e.g useful if you want to send a user back ( window.history.back() )
Image - An image on your document. Useful if you want
to change the image from your script, e.g. rollover.
Location - Contains information on the current URL. Allows
you to access different parts of URL, doing reload etc. e.g. if you
want to send user to the new URL you can do something like:
window.location.href="http://www.yahoo.com/";
window - Represents a browser window or frame. This is
the top-level object for each document. Have a lot of useful properties &
methods. Examples of usage: window2 = window.open(...); - opens the new window window.close(); - closes window window.confirm("Are you sure?"); - gets the confirmation from the
user.
Note, since window is a top-level object for the client hierarchy, many
methods can be called directly, it'll be assumed, that these are the methods
of current window.
e.g. confirm("Are you sure?"); will work just fine.
Some examples of useful things you can do with JavaScript can be found
here.
Summary:
JavaScript is extremely useful for building the interactive Web
interfaces.
It is also very useful in verifying the user's input. It can be pretty
frustrating for the user to submit the form with 10 fields just to find out,
that he forgot to put the zip code, press the back button and then see, that
he has to enter everything again... - Avoid it by checking such things
on the browser side using the JavaScript. Note here, that it's for
the user's convenience only, on the server side never trust the browser! See
more in security part for the
reason 'why'.
Sometimes you can even get some work done on the client-side.
e.g. if you
have a screen, where user chooses the items from the list of products, you
can calculate a total and update it each time, he/she adds or deletes an
item. Although, as it was said in the previous paragraph, don't trust these
numbers for the real transactions! Also remember, that JavaScript is the
scripting language and is not intended for the huge programs, so even though
you can write some big client-side application with this, it's not such a
good idea :-)