Web Systems Lecture 5  - JavaScript

This lecture is divided into hyperlinked sections

Introduction
What is JavaScript?
How does JavaScript differ from Java?
Core JavaScript
Client-Side JavaScript
Server-Side JavaScript
JavaScript and the ECMA Specification
Conclusion
Tutorial Questions
References


Introduction

This section of the course is concerned with JavaScript, a scripting language for adding functionality to web pages.

We will see what JavaScript is and how it differs from Java.

We will see how Client-side and Server-side JavaScript differ from each other.

We will see how JavaScript is being standardised to provide a world-wide standard


What is JavaScript?

JavaScript is a cross-platform, object-oriented scripting language. Core JavaScript contains a core set of objects, such as Array, Date, and Math, and a core set of language elements such as operators, control structures, and statements.

JavaScript lets you create applications that run over the Internet. These applications may run on the client or the server.

Client applications run in a browser, such as Netscape Navigator or Internet Explorer.

Server applications run on a server, such as Netscape Enterprise Server. Using JavaScript, it is possible to create dynamic HTML pages that process user input and maintain persistent data using special objects, files, and relational databases.


How does JavaScript differ from Java?

Both Java and JavaScript were written by Netscape. JavaScript appears similar to Java as much of the code is borrowed from Java.

Some fundamental differences do exist, however.

JavaScript is embedded in the HTML that is used to write a document whereas Java is used to create applets (small applications) that are accessed from within web pages, called upon by HTML code.

The JavaScript is interpreted by the browser. This means that it is translated a line at a time into the 1s and 0s that perform the various tasks. Java is compiled into bytecode that is stored on a web server. This bytecode is transferred to your computer and executed on the client.

JavaScript is a very free-form language compared to Java and is object oriented. You do not have to declare all variables, classes, and methods. Java is much more formal in the approach required to compile an applet, requiring all variable data types to be declared.  Java's class-based model means that programs consist exclusively of classes and their methods.


Core JavaScript

Client-side and server-side JavaScript have the following elements in common:

     Keywords

     Statement syntax and grammar

     Rules for expressions, variables, and literals

     Underlying object model (although client-side and server-side JavaScript have different sets of predefined objects)

     Predefined objects and functions, such as such as Array, Date, and Math


Client-Side JavaScript

Client-side JavaScript extends the core language by supplying objects to control a browser (Navigator or another web browser) and its Document Object Model (DOM). For example, client-side extensions allow an application to place elements on an HTML form and respond to user events such as mouse clicks, form input, and page navigation.

The client may download a HTML page containing JavaScript statements that request the user to fill in a form and also to validate the contents of the form. If the information entered by the user is of the wrong format, e.g. letters instead of numbers in a telephone number field, the entered data can be checked locally by the client instead of being transferred back to the server for verification. A warning message can be displayed to the user by the client too to alert him or her to the error. This saves time and network bandwidth.


Server-Side JavaScript

Server-side JavaScript extends the core language by supplying objects relevant to running JavaScript on a server. For example, server-side extensions allow an application to communicate with a relational database, provide continuity of information from one invocation to another of the application, or perform file manipulations on a server.

In contrast to pure client-side JavaScript pages, HTML pages that use server-side JavaScript are compiled into bytecode executable files. These application executables are run by a web server that contains the JavaScript runtime engine. For this reason, creating JavaScript applications is a two-stage process.

Stage 1

You create HTML pages (which can contain both client-side and server-side JavaScript statements) and JavaScript files. You
then compile all of those files into a single executable.

Stage 2

A page in the application is requested by a client browser. The runtime engine uses the application executable to look up the source page and dynamically generates the HTML page to return. It runs any server-side JavaScript statements found on the page. The result of those statements might add new HTML or client-side JavaScript statements to the HTML page. The run-time engine then sends the resulting page over the network to the Navigator client, which runs any client-side JavaScript and displays the results.


JavaScript and the ECMA Specification

Netscape invented JavaScript, and JavaScript was first used in Netscape browsers. However, Netscape is working with ECMA (European Computer Manufacturers Association) to deliver a standardized, international programming language based on core JavaScript. ECMA is an international standards association for information and communication systems. This standardized version of JavaScript, called ECMAScript, behaves the same way in all applications that support the standard. Companies can use the open standard language to develop their implementation of JavaScript. The first version of the ECMA standard is documented in the ECMA-262 specification.

The ECMA-262 standard is also approved by the ISO (International Organization for Standards) as ISO-16262. You can find a PDF version of ECMA-262 at Netscape DevEdge Online. You can also find the specification on the ECMA web site. The ECMA specification does not describe the Document Object Model (DOM), which is being standardized by the World Wide Web Consortium (W3C). The DOM defines the way in which HTML document objects are exposed to your script.


Conclusion

JavaScript is a scripting language used to bring functionality to web pages.

It has a core set of features which are supplemented in client or server-side JavaScript.

Client-side allows statements to be interpreted and some processing e.g. field validity to be performed on the client.

Server-side JavaScript is compiled into bytecode that is run on a JavaScript runtime engine. This engine can create web pages for requesting clients on the fly.


Tutorial questions

Why would you use JavaScript in some situations, but need to use Java in another?

Describe 2 different applications one of which would require JavaScript and the other Java.

Explain why one is suitable rather than the other.


References
http://developer.netscape.com/docs/manuals/js/client/jsguide/intro.htm
 
 



(c) MM Clements 2001                                                    Back to top of Page