Wednesday, September 1, 1999

Dynamically generated JavaScript libraries

WEBMASTER'S TOOLKIT

By Jeffrey R. Burrows

JavaScript may not have had the media attention that Java has had for the past five years, but it's done more than its glorious namesake to revolutionize Web page design. JavaScript is the glue that holds many Web sites together, especially the user input forms necessary for e-commerce. For Notes developers, its pervasiveness in R5 means an alternative to some of LotusScript's dead-ends and a common development method for Notes and Web clients.

The good news for script developers is that Microsoft's dalliance with VBScript, and its proprietary JScript flavor of JavaScript have given way to a new cross-platform standard called ECMAscript.

The bad news is that different versions of JavaScript proliferate. What's worse, since the interface between script and the browser -- the document object model -- can differ greatly between browsers, developing a Web page that will operate for any browser, or even any recent browser (forgetting Lynx, Cello and Mosaic users), is a real challenge for Web developers.

The challenge

With the growing reliance on JavaScript comes a need to manage the code, cope with browser incompatibilities, protect valuable code from unauthorized copying, and reuse handy functions in other Web pages.

The simplest method of delivering script functions to browsers is to include it in the Web page itself. The HTML tags <SCRIPT> and </SCRIPT> surround the functions, which can then be called in, for example, on click events. This is easy to grasp, but means the whole world can see your code and when you change a function, every Web page with that function must be manually updated. Furthermore, the functions either have to do all the work of adapting to different browsers, or depend on a particular Web browser and model.

Static script libraries

To overcome some of these problems, Netscape Navigator and Internet Explorer (in their 3.x incarnations) introduced the concept of script libraries. Instead of placing the script functions between <SCRIPT> and </SCRIPT> tags on the Web page itself, the code is contained in an entirely separate file, which is loaded by a SRC parameter on the <SCRIPT> tag, like this:

<SCRIPT SRC="../js/validate_library.js"></SCRIPT>

The SRC parameter could refer to a local file on the Web server, as above, or be an absolute URL to a library on another server entirely. For Domino programmers, this URL could refer to a page element (for R5) or a Notes document in a view (for R4) containing script code. The library itself is static, but can be reused in its entirety between Web pages, and centrally updated. Multiple libraries can be used on the same page using multiple &lt;SCRIPT SRC&gt; tags.