Chapter 23. Scripting Java Applets and Flash Movies


A plug-in is a software module that can be "plugged in" to a web browser to extend its functionality in some way. Two of the most widely deployed (and, not coincidentally, the most powerful) plug-ins are the Java plug-in from Sun Microsystems and the Flash Player from Adobe (which acquired Macromedia). The Java plug-in enables a browser to run applications known as applets written in the Java programming language. The Java security system prevents untrusted applets from reading or writing files on the local system or doing anything else that could alter data or compromise privacy. Despite the security restrictions that are imposed on applets, the Java plug-in ships with a huge library of predefined classes that applets can take advantage of. This library includes graphics and GUI packages, powerful networking capabilities, XML parsing and manipulation packages, and cryptographic algorithms. Java 6, in prerelease at the time of this writing, will include a complete suite of packages for web services.

The Flash Player is extraordinarily popular and almost universally deployed. It is a virtual machine that interprets "movies," which may include true streaming video but typically consist of animations and rich GUIs. Flash movies may include scripts written in a language known as ActionScript. ActionScript is a variant of JavaScript, with the addition of object-oriented programming constructs such as classes, static methods, and optional variable typing. ActionScript code in a Flash movie has access to a powerful (though not as extensive as Java) library of code that includes graphics, networking, and XML manipulation capabilities.

The term plug-in doesn't really do justice to Java and Flash. These are not simple add-ons to the browser; they are both suitable for application development in their own right, and both can provide a richer user experience than DHTML-based web applications do. Once you realize how much power these plug-ins bring to the web browser environment, it is only natural to want to tap that power with JavaScript. Fortunately, you can do exactly that. JavaScript can be used to script both Java applets and Flash movies. Furthermore you can even do the reverse: Java applets and Flash movies can call JavaScript functions. This chapter explains how to make it work. Be prepared, however: the interfaces between JavaScript, Java, and ActionScript are awkward, and if you do any serious scripting of Java and Flash, you will encounter incompatibilities, bugs, and frustration.

This chapter begins by explaining how you can use client-side JavaScript to script Java applets. (You may remember that Example 22-14 scripted a Java applet to create client-side graphics.) Next, it explains how, in Firefox and related browsers, JavaScript code can script the Java plug-in itself, even when no applet is present. (This technique was shown in Example 22-16.)

After explaining how to script Java with JavaScript, this chapter goes on to explain how to create applets that read and write JavaScript properties and invoke JavaScript methods, including applets that use the Java version of the DOM API to interact with the document displayed by the web browser.

Java and JavaScript are also covered in Chapter 12. That chapter was very different, however. It described how to embed a JavaScript interpreter in a Java application and how to allow scripts run by that interpreter to interact with a Java program. Chapter 12 did not cover client-side JavaScript nor did it cover applets. It did cover LiveConnect, a technology that enables JavaScript to communicate with Java, and the material on LiveConnect from that chapter is relevant to this chapter. Note, however, that the features described in Chapter 12 as being specific to the "Rhino version of LiveConnect" do not work with client-side JavaScript and applets.

The Java sections of this chapter assume you have at least a basic familiarity with Java programming. If you do not use applets in your web pages, you can skip them.

After covering Java, I turn to the topic of scripting Flash: allowing JavaScript code to invoke ActionScript methods defined within a Flash movie and allowing the ActionScript code in a movie to call JavaScript code. I discuss this material twice, first covering techniques that work in all recent versions of Flash and then covering a much simpler new technique that works only in Flash version 8 and later.

Because Flash is so powerful, it was used in earlier chapters of this book. In Chapter 22, the Flash Player displayed dynamic client-side graphics with the help of some simple ActionScript code (Example 22-12) in a Flash movie. And in Chapter 19, Example 19-4 took advantage of the Flash Player's client-side persistence mechanism.




JavaScript. The Definitive Guide
JavaScript: The Definitive Guide
ISBN: 0596101996
EAN: 2147483647
Year: 2004
Pages: 767

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net