JavaScript and Chemistry*
Departamento
de Química, Faculdade de Ciências
e Tecnologia,
Universidade Nova de Lisboa, 2825 Monte da Caparica, Portugal
E-mail: jas@mail.fct.unl.pt
![]()
Introduction
JavaScript is rapidly becoming omnipresent on the WWW (World Wide Web) making more sophisticated and intelligent websites a reality. Scrolling text on the middle or on the bottom of a webpage, opening a small window by clicking on a "link", executing a simple calculation in a WWW window... all these actions are not the result of HTML programming but can be implemented using JavaScript.
In this article I'll try to briefly explain what JavaScript is, how it works and show how it has been applied to chemical needs.
![]()
What is JavaScript?
JavaScript is a programming language used in WWW documents. It can be embedded in the HTML source code and directly interpreted and executed by the browser. Although not all browsers support JavaScript, Netscape Navigator (2.0 or later) and Microsoft Internet Explorer (3.0 or later) do. According to the browser stats of the Department of Chemistry of the Imperial College of Science in London, 95+% of their visitors use at least one of these browsers. This is a good indication that JavaScript is a good cross-platform, cross-browser programming language.
It enables applications to be run locally by the browser, making HTML documents become more "intelligent". Some JavaScript applications may also be executed on the (Netscape) server side instead of the browser. This is called LiveWire JavaScript and it works in a similar fashion to CGI (Common Gateway Interface). More information about "Netscape's Secret Weapon" can be found at: http://www.homefair.com/late96/javascript.html?NETSCAPE_LIVEWIRE.src=&NETSCAP E_LIVEWIRE.a=ok
JavaScript is often confused with Java. JavaScript was developed by Netscape, whereas Java is a programming language developed by Sun Microsystems. Actually, Java was originally written to program 'smart' appliances such as TV remote controls and toasters (!) but found a new use on the WWW because of it's cross platform compatibility.
Although both languages have a WWW interface, similar expression syntax and basic control-flow constructs, they are fundamentally different. Java is a sophisticated, compiled programming language. This means that the source code of a Java application must be converted using appropriate software into a binary file before it can be interpreted by a Web browser or other Java interpreter. On the contrary, JavaScript code is directly interpreted by the browser making it slower than compiled languages such as Java, C, FORTRAN, etc. and not suitable for computationally intensive tasks.
Because of security concerns, both Java and JavaScript cannot automatically write to hard disk. If this were not so, the data in the disk could be damaged or one's privacy invaded by an application that is placed on the hard disk while one is navigating through an apparently innocent webpage. For those concerned about security, more information can be found at http://www.cs.utah.edu/~gback/javasec/
![]()
How does JavaScript work?
It is better explained with an example. The HTML lines below (with JavaScript embedded statements) implement a very simple molecular weight calculator in a WWW document. Below it you will find how the code is interpreted by a Javascript enabled browser:
<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
<H2>Molecular Weight Calculator</H2>
<FORM NAME="form">
C:<INPUT NAME="c" size=3> +
H:<INPUT NAME="h" size=3> +
O:<INPUT NAME="o" size=3> =
Mol.Wt.:<INPUT NAME="result" size=8><p>
<INPUT TYPE="button" VALUE="Calculate" onClick="document.form.result.value
= 1.00794 * document.form.h.value + 12.011 * document.form.c.value + 15.9994
* document.form.o.value">
</form>
</BODY>
</HTML>
This is what it will look like:
When running this applet (an applet is a small Java or JavaScript application), you should insert the number of atoms of each element (Carbon, Hydrogen and Oxygen) in the molecule and then click on the "Calculate" button. The molecular weight will be calculated and displayed. You could obviously incorporate the code in a much more sophisticated WWW document that includes links, colours, images, tables, etc.
As you can see, JavaScript provides an extraordinarily easy way to create applications that do not require a specific computer platform nor a compiler and that execute in the user friendly WWW- interface style.
For those interested in learning JavaScript, I recommend Netscape's "JavaScript Authoring Guide". And for an excellent directory of Java (and JavaScript) resources, see the Gamelan website at: http://www.gamelan.com/
![]()
Chemical Applications of JavaScript
Although interesting chemical JavaScript applets have been designed, there are not many yet. Following are some interesting chemical applications of JavaScript that can be found on the WWW:
Molecular
Weight/Percentage Calculator
By Yue-Ling Wong
Calculates the M.W. and percentage of C, N, O, H given the number of atoms
of each element.
Simple
Periodic Table
by F. Senese
A Periodic Table is displayed and, by clicking on an element, the elements
properties are presented in the status line of the browser.
Buffers
for pH Control
by Rob Beynon
If you need a buffer solution you just need to insert the buffer species,
volume, pH, concentration, ionic strength and temperature and the recipe
will be generated.
Quantum Chemistry
Examples
by David Mosley
http://www.chimie.fundp.ac.be/javas/he_dz_calc.html
The SCF-LCAO calculation of the ground (1s)2 state of the helium atom in
a double zeta basis of Slater-type orbitals.
http://www.chimie.fundp.ac.be/javas/fsgo_calc.html
Calculation on the H2 molecule using a subminimal FSGO basis set.
JavaScript with Chime
plug-in (3D models)
Used in conjunction with molecular coordinate files and Chime plug-in,
JavaScript offers powerful techniques to visualise 3D molecular species.
For example, different sequences of Chime actions (such as rotations) or
animations may be programmed and linked to different strings or images.
The following two examples are really spectacular and beautiful. They require
the Chime plug-in which is available free from: http://www.mdli.com/chemscape/chime/download.html
The
photosynthetic reaction centre from the purple bacterium Rhodopseudomonas
viridis
by Peter Nixon, Christopher Leach and Henry S. Rzepa
XMol
XYZ MultiFrame Animation files
by Raul E. Cachau