Javascript has historically been maligned by "real" programmers as a "fake" language, often for reasons that are no longer applicable. Since its original publication Javascript, has developed into a complex and versatile language and has been used to drive many of the most impressive web applications on the internet (GMail, Google Maps, the new Hotmail, etc.). Javascript must be used within the context of a web browser (or something built to interpret Javascript code like a web browser), but, the web browser has, of late, become one of the most popular platforms for new programs, and so this does not impose any significant limitation for our purposes.
When one goes to a webpage which uses Javascript, the source code for the program is downloaded to the user's computer. The user's machine is then responsible for interpreting and processing this code. This means that one should write Javascript code with the understanding that it may be run on a machine significantly older and slower that one's one. Also, because of security issues involved in running an external program on a personal machine, Javascript, does not allow you to write or open files (that is, you cannot save any work performed in a program written solely in Javascript). Javascript does, however, interface very nicely with other languages like PHP, which can write files. We will be looking at PHP next week.
You can place Javascript code in the HTML document itself or in a separate file (probably the most common professional option). If you place it in the HTML document, the code is enclosed in a <SCRIPT> tag with the attribute "language='Javascript'".
If you want to include the javascript in a separate file, include a "src" attribute in the script tag which points to the script file.
One of the most useful commands in writing Javascript is the "alert" command, which causes the page to open a new window containg a message (a window not usually blocked by pop-up blockers). You will probably remove it from your final drafts, but it is indispensible as a debugging tool.
The syntax for the command is as follows:
In the above example, the Javascript will display two alert messages, one right after the other. Notice that I ended the line with a semi-colon; Like CSS, all commands in Javascript terminate with a semi-colon.
If you've done any programming at all, you are familiar with the concept of variables. Variables are like the style attributes in CSS. They are named entities that store a value. In a lot of programming languages, the variables have types. For instance, an integer (whole number) is stored in a different sort of variable than a float (a number with a decimal point), and both are different from a String (a series of letters and numbers). This means that it is easier to make mistakes (there is nothing on the coding side that will keep you from trying to substract 42 from Kittens), but you can code a little more quickly.
Variables are created by typing "var variableName" and set by typing "variableName = variableValue". You can also create and set a variable at the same time as shown below.
In the above example, the variable myName is created and set to "Doug." The variable my age is created and set to 28. I then have Javascript alert me of the value of both variables. I did not put quotation marks around the value 28 because it is a number. I also left quotation marks out of the alert statements because I wanted the value of myName rather than the word "myName" to be printed in the alert box.
You can also perform arthimetic on variables. For instance:
A common structure in programming is the IF statement. The IF statement marks a fork in the path of a program. The truth of a statement is evaluated, and the result determines which path the program takes.
Consider the following:
The statement after "if" always evaluates to a special type of variable called a Boolean (after boolean logic). A Boolean variable has only two values, "true" and "false". "True" and "false" are also reserved words in Javascript, so you even write something like this:
Most of the time, though, you will be writing a statement which compares two things. The symbols for comparison are as follows:
Note also that the logical statement which the if statement evaluates is enclosed in parenthesis. You always need at least one pair of parentheses, but you can have more. For instance:
You can also allow for more than two scenarios using an "else if" statement as follows:
Another common structure in programming is the loop. There are many types of loops, but the most common are "WHILE" and "FOR" loops. WHILE loops instruct the program to do something until some condition is met. FOR loops tell the program to do something to every one of a set of something. Javascript's syntax for loops is based on that used in Java (which is, in turn, based on C). The syntax can be confusing and highly symbolic, but once you get the hang of it it can be written very quickly.
Consider the following examples
In the second loop, the program will continute to execute the code in the curly brackets as long as the variable name does not equal "Doug" (at the beginning of the loop, name is set to "Sherlock Holmes"). Within the loop, the variable counter is checked to see if it equals 2. If it does, name is set to "Doug." In any case, counter is incremented by one at the conclusion of each lap through the loop.
You will eventually want to separate sections of code into chunks known as "functions." Functions are named block of code which are not run until they are called from within the program. Functions often have input variables which are set to initial value. These input variables are listed in parentheses after the function name. Consider the following:
Functions also return a value. The function will evaluate as this value if used in a logical statement or if a variable is set to equal a function. By default the value is "null," but you can return something different with the "return" command.
Consider the following:
Ok, now comes the fun part. Javascript functions can also be called by user actions on the page (clicks, double clicks, mouse overs, etc.). These actions are called EVENTS, and they must be captured by the page itself. The traps for events are placed in the attributes of tags. For instance, let's create a page that throws an alert after the page loads:
Javascript can actually create HTML elements, and HTML elements can execute Javascript functions. Consider the following example:
The first line (document.getElementbyId) in the function "createPage()" above gets the element with
the attribute "pageContent" which we created in the BODY of the HTML.
The second line "createElement", creates a DIV element in the current page. It is not, however,
attached to the page yet, so anything we put in it won't show up.
The next three lines define the attributes of the new element (in this case "id", "class", and
"onclick"). Note that the function associated with "onclick" takes an input value of "this" which
is a reserved word which refers to the element associated with the event.
A new text element (or node) is then defined and attached (via appendChild) as a child to pageMessage, which is,
in turn, attached as a child to pageContent.
The onclick event calls the function clicked which changes the text color of an element to red. Note that the syntax for changing the CSS values of a particular element is
Take a short text and encode it HTML. Use the <SUP> tag to create super-script footnote makers (like this1). Create a div element with id "footnote" and give it the CSS attribute of "display: none". Give the "sup" tag an onClick event which causes the div element's style to change to "display: block;" and give this div a text child with an explanatory note. Allow the footnote div to be closed (made invisible) and use the removeChild() function to get rid of the current footnote so you can display another. If you're up for a real challenge, tag the text in TEI at first, and then use an XSLT to transform it into an HTML document with an external Javascript file.
to be displayed with the text of the footnote.