JavaScript is an example of a scripting language that makes it possible for a browser to run program code similar to that found in fully-fledged programming languages. Some of the applications of scripting languages include processing data supplied by the user, responding to events generated by the user and making web pages more interactive and dynamic.
JavaScript is often used to process data collected from a form. Here is a simple example of a form with a button which requires no keyboard input, only a click on a button:
What is the meaning of école?
The JavaScript function responds to the 'onClick' event by calling a function called Translation(). The Translation() function is defined in the Script section of the document which is generally placed between HEAD and /HEAD.
<HEAD>
<TITLE>Introducing JavaScript</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function Translation () {alert("School"); }
</SCRIPT>
</HEAD>
Javascript is written in one of two ways:
A function begins with the word function and a pair of brackets, which must be present. The brackets can be empty or they can contain one or more parameters, that is data objects which are used inside the function. The body of the function is contained within braces - { } - and it is here that programming proper begins.
The program code here uses a pre-defined method to output a message to an 'alert' box.
The JavaScript function in this case is called from within the <FORM> and </FORM> tags. Form buttons can call JavaScript functions from the onEvent structure. In this case the event is a click on the button. JavaScript has a number of events which follow the same pattern as this one.
<FORM> <INPUT TYPE="button" VALUE="Translate" onClick=Translation()><P> </FORM>
All of this follows a path recognisable from other programming environments:
In a command-line environment (such as DOS) the user enters values onto a line of text. In a windows-style environment data are entered through areas of the screen set up by the programmer for input and output. Much of the effort involved in writing windows-style software is taken up with arranging the areas and means of input and formatting the areas of output.
HTML and Javascript together provide a range of options for these standard programming tasks. The range of options is much narrower than those found in full-scale programming languages like C++ and Visual Basic but this situation may change in the future as more facilities are added to browser scripting languages.