2. Web Presentation Technologies

Java Script

Author: Russell Bateman

Objective: Use JavaScript to customize display of HTML.


A Very Brief Introduction

JavaScript, which has little or nothing to do with Java itself, was an innovation of engineers at Netscape and is also claimed to be owned by Sun Microsystems, Inc. Whatever the case, it is a way of doing dynamic webpages in which the dynamism occurs completely at the browser rather than on the back end via CGI, Perl, PHP, Java servlet or any other means.

The ability to host pages containing embedded JavaScript commands is a function not of the server host holding the webpages, but of the browser use to view them. All modern browsers speak JavaScript, but there are minor differences between their behavior and Microsoft sneakily mixes in Active-X in such a way that you must be careful when writing JavaScript and testing it with Internet Explorer (IE) that you don't end up with JavaScript that only does what you want it to do in IE (and not in other browsers).

What JavaScript does share with Java is its object orientation. Its syntax seems arguably to be an inspiration of the C/C++ language (but then, so does Java's syntax). One major difference between JavaScript and C/C++ is that statements do not need to end with a semicolon, however, they can (and so you can guess whether I end them that way or not).

With JavaScript embedded inside an HTML document, it is possible to perform virutally limitless application antics such as creating and sizing new windows as well as populating them with new content. You could implement a Celsius-Fahrenheit temperature conversion utility that runs in the browser.

You will see JavaScript mostly at the top of HTML documents (in the header) though it is by no means limited to appearing there. In fact, it is embedded through the leniency of the HTML <script> tag:

   <head>
      <script language="JavaScript">
      <!--
      commented code here...
      // -->
      </script>
   </head>

Of course, the fact that there is JavaScript code in the HTML header doesn't preclude the other tags that are usually found in a header like <style>, <title>, etc.

Good examples of JavaScript coding more or less transcend the introductory scope of this presentation. You will find ample examples in the tutorials.


Links


Here is an example of JavaScript that is directly embedded in the HTML code—inside a <form> in fact. This is a rather dynamic example of JavaScript and HTML in that a form is usually posted to a CGI or other server-resident application for interpretation and the result, if any, is resent to the browser as a new HTML page to replace the one which contained the orignal form.


Modify either value and click Enter or Tab

Fahrenheit:

Centigrade: