JavaScript Section

JavaScript is a language with object oriented capabilities that can be used for client-side scripting on the Internet. This means that browsers that support JavaScript can run JavaScripts provided on a certain server locally after they have been downloaded. A browser that support JavaScript is supplied with a JavaScript interpreter to process the statements carried out within the several JavaScripts. In a way JavaScript can be seen as "glue" between several parts of a homepage, making the page more dynamic or in some cases interactive.

JavaScript can be used for form validation for instance. It is possible to validate the values a user has entered into a web form before the form is submitted. The benefit of this approach is that already on the client-side a validation is done and if that validation fails so will the server-side validation, because at least that validation has to reside on the server-side as well. Preferably a better check will be performed on the server-side. But with client-side validation it is possible to avoid that data, which is definately incorrect is sent across the internet, validated by the server, and sent back to the user because it is wrong anyway.

To make it more clear: Why submit a form to a server if on the client side already is known that the form was filled in wrong? So with client-side validation it is possible to have the user fill in proper values. Because client-side validation is not completely reliable and the user can turn off JavaScript in his browser there has to be a server-side validation as well. But at least with client-side validation you can reduce the internet traffic and ofcourse client-side validation is quicker than server-side validation. So the chance that the user will get an error message back from the server saying that the form was filled in improperly becomes smaller in that way.

JavaScript often is used to make a site just look a little bit nicer. Many sites have a JavaScript menu or navigation and use mouseover images to make the page appear nicer. The control of these images often is done using JavaScript. JavaScript has some event handlers that can take care of the swapping of the images based on several events such as mouse over, mouse out or mouse pressed.

The problem with JavaScript is that there are many versions available. In a way that is not bad, because for every version of JavaScript a specification is available. If the author of the JavaScript specifies in a proper way what version of JavaScript is used and the syntax and functions are used properly there shouldn't be a problem. The problem lies in the fact that the best known browsers (Microsoft Internet Explorer and Netscape) have their own interpretation of JavaScript and have added their own functions, events etc. to the language. So many solutions work in Microsoft IE and not in Netscape vice versa. As a result of this many scripts have to take this into account and have to detect what browser is used to load the script. Based on the browser certain code is executed and other code has to be skipped. At a certain point the script consists of many if-statements having to do with behaviour for a certain browser.

It is very easy to detect the browser with basic JavaScript using the navigator object. This is one the standard objects available in JavaScript. Other basic objects are window and document. The names of the objects tell what their purpose is. The navigator object handles some properties of the browser that is used to access the page, while the window object handles with the window of the browser that is used to display the page and the document object has to do with everything that is part of the actual content (and source code) of the document that currently is displayed.

The window object has many functions. Two of them are so basic that everybody writing JavaScript has to know them: alert() and confirm(). The alert function will produce a modal dialog box with a single "OK" button to display a message to the user, while the confirm function will produce a modal dialog box with an "OK" and "Cancel" button that can be used to ask the user a question or let him confirm some statement. If "OK" is pressed by the user, the method will return true and if "Cancel" was pressed false. If the code is written well the execution of the process that was initiated should be stopped if the user has pressed the "Cancel" button.

Hello World

The alert method can be used to write a very simple message. The code below is an example of this:

alert("Hello World");

The simple line of code above will produce a dialogbox similar to the one displayed next to this text. The dialogbox is modal and the only way to get rid of this box is to press the "OK" button. Generally these kind of dialogboxes are used to notify the user of something. So these boxes should be used to display a message.


Links

Contact the author: Send an electronic mail to: pajtroon@dds.nl.
Peter's ICQ Number is: #3900785.

This page: Copyright © 2002 - 2005 Peter A. J. Troon

Note: This page is part of the Peter Troon Site.