Chapter 12. Executable Content

One of the most useful web technologies is the ability to deliver applications directly to the browser. These typically small programs perform simple tasks on the client computer, from responding to user mouse or keyboard actions to spicing up your web page displays with multimedia-enabling software.

You can embed scripts in your documents using a language known as JavaScript. Or you can load and execute small, Java-based, platform-independent applications known as applets. During execution, these programs may generate dynamic content, interact with the user, validate form data, or even create windows and run entire applications independent of your pages. The possibilities are endless, and they go far beyond the simple document model originally envisioned for HTML.

In this chapter, we show you, with simple examples, how to include two kinds of executable content—scripts and applets—in your documents. We won't, however, teach you how to write and debug executable content. This is a book about HTML and XTHML, after all. Rather, get an expert opinion: turn to any of the many excellent texts from O'Reilly, especially the companion JavaScript: The Definitive Guide, by David Flanagan.

Applets represent a shift in the basic model of web communications. In most other web applications, servers perform most of the computational work, client browsers being not much more than glorified terminals. With applets, web technology shifts to the client, distributing some or all of the computational load from the server to the client computer and its browser.

Applets also represent a way of extending a browser's features without forcing users to acquire new browsers, as is the case when developers implement new tag and attribute extensions to HTML. Nor do users have to acquire and install a special application, as is required for helper or plug-in applications.[*] This means that once users have a browser that supports applets (all the currently popular ones do), you can deliver applets directly to the browser, including display and multimedia innovations.

Java-based applets—web page-referenced programs retrieved from a network server and executed on the user's client computer—are an example of what the HTML 4 and XHTML standards call inclusions. As with images, the browser first loads the HTML document, then examines it for inclusions—additional, separate, and discrete content that the client browser is to handle. A GIF image is one type of inclusion. A .wav sound file, an MPEG movie, and a Java-based clock program are other types.

The HTML 4 and XHTML standards generally call the inclusion contents objects. In fact, in your document you may identify and load nearly any object file over the network through a universal <object> tag, discussed in detail shortly in section 12.2.1.

Once the object has been downloaded, the standards dictate that the browser somehow render the object, by internal or external mechanisms. Otherwise, plug-ins and other helper applications may provide the necessary rendering mechanism. Internet Explorer, for example, has its internal resources play an AVI movie, whereas other browsers rely upon some third-party software, such as RealPlayer or QuickTime, to show the movie.

Creating Java applets is a programming task, not usually a job for the HTML or XHTML author. For details, we recommend that you consult any of the many Java programming texts, including those from O'Reilly.

Developed by Sun Microsystems, Inc. of Mountain View, California, Java supports an object-oriented programming style wherein classes of applets can be used and reused to build complex applications. One would think that applets written in the same language should run in any browser that supports Java. As is so often the case, reality is more complex. Until Netscape 6 and Internet Explorer 6, browsers included their own Java Virtual Machines (JVMs), and their implementations, especially Microsoft's, could be quirky. Certain Microsoft implementation decisions in Internet Explorer 4 and earlier caused some valid Java applets to fail when running. Microsoft fixed these problems with Internet Explorer version 5 but, because of its lawsuit with Sun, chose not to include a JVM in Internet Explorer 6.[*] Although this may sound like bad news for applets, in fact, Internet Explorer 6 prompts you to download Microsoft's JVM. Sun's Java Plug-in is free over the Internet. Users of any browser can install the Java Plug-in to get state-of-the-art Java support.

We should take this opportunity also to mention ActiveX, an alternative executable content technology originally developed by Microsoft. ActiveX itself is proprietary, closely coupled with various versions of Microsoft Windows, and Microsoft's plug-in works only when used with Internet Explorer, though alternative plug-in implementations now exist for all the popular browsers.

ActiveX controls (as they are called) run on browser versions targeted to various versions of Windows, but a single ActiveX control will not run on these different versions without recompilation. This is in contrast with Java applets; a single Java applet can be written and compiled once and immediately run on a broad range of browsers and operating systems.

ActiveX also presents an unacceptably high security risk to any user whose browser supports ActiveX technology. [] Though over the years security has gotten better, it is ridiculously easy to penetrate and damage a computer running a browser that allows ActiveX applets to be executed. In fact, all the popular browsers, Internet Explorer included, let users explicitly block ActiveX applets. For this reason, we cannot recommend ActiveX as a viable applet implementation technology and we go so far as to recommend that users disable ActiveX capability within their browsers.



[*] Actually, Internet Explorer 6 users must download and install Java support. Read on for details.

[*] As we wrote this, even this situation may change, with Microsoft reversing itself and deciding to include a JVM in a service pack for Windows XP. There is still no sign of default inclusion of a JVM in Internet Explorer 6 downloads, however.

[] You can find a good description of the risks at http://www.digicrime.com/activex.