Using Javascript in Web Pages
Javascript is a key technology to exploit in the development of Web pages, since it adds interactivity
and responsiveness to a Web site. Even if you do not know how to program, it is possible (but possibly painful) to integrate free Javascript
programs into a site.
Because Javascript runs on the client side (on the user's computer rather than the Web server), it is used for interactive effects,
on-the-fly validation of data in forms, and other applications. The most common usage of Javascript is to create menus and other navigational mechanisms. Another common use is creating image
rollovers, where moving the mouse over the first image is detected by the browser, which uses Javascript
code to replace the first image with another. When the mouse moves off the second image, Javascript code
replaces the second image with the first.
There are several large advantages offered by the use of Javascript:
- Javascript can watch specific events in the browser, and trigger code to run when conditions are met. For example, when the mouse hovers over this box, run the function called HighlightBox.
- Javascript can be used to validate the data in form fields, select dates from popup calendars, etc.
- Javascript can directly access the browser's internal representation of a Web page (the DOM, or Document Object Model). This means that a Javascript program can pick out any element on your page for direct manipulation -- deleting, adding, changing the style information, anything. The ability to change any element on the page programmatically opens the door to completely interactive pages.
- In the last few years, a new technique called Ajax has been introduced -- this is core to the whole Web 2.0 concept. Using the Ajax technique, Javascript in the user's Web browser opens up an HTTP
connection (the protocol whereby Web browsers and Web servers interact) to the server. The responses
from this interaction can arrive asynchronously (at any time after the request, but not necessarily immediately),
and the Ajax mechanism allows Javascript to insert the results directly into the page without a page reload. This is a simple but powerful technique that allows for greater interactivity in pages because the user does not have to wait for
a page reload every time something happens.