|
|||
|
Click Here! |
Chapter 14From JavaScript to Java-Looking into the Future
CONTENTS
Now that you've mastered the essentials of JavaScript, it should be clear that JavaScript is a powerful tool for extending the functionality of basic HTML documents and creating sophisticated interactive applications. Nonetheless, the questions remain: How do I do more? Can I move beyond JavaScript and extend its power? In this chapter, we take a look at the relationship between JavaScript and Java and how you can quickly and easily add Java applets to your pages today. We will also look at how JavaScript can interact with Navigator plug-ins. This intercommunication between Java, JavaScript, and plug-ins is known as LiveConnect, a feature of Navigator 3.0. In Navigator 2.0, direct communication between Java and JavaScript was not possible. With Navigator 3.0, Netscape has added the applets array, which provides a mechanism for communication between JavaScript and Java. This chapter first looks at the limited interaction possible in Navigator 2.0, and then introduces the applets array available in Navigator 3.0. We will go on to look at interacting with plug-ins in Navigator 3.0. You'll learn about the following:
Integrating Java into JavaScript-The applet ObjectWhen Sun and Netscape announced the creation of JavaScript in late 1995, they made a lot of noise about the role of JavaScript in gluing Java into Web pages. Java applets, because they exist outside the context of the Web page itself, are unable to interact with the type of document, form, and window objects that JavaScript can work with. Java applets are simply assigned a space in the current page, like images are given a particular rectangle, and then they do their thing in that space. Any interaction with the user requires the applet to provide its own alternatives to the HTML forms and links that JavaScript can so readily work with. Given this, JavaScript's role is supposed to become the link. By having access to all the document and browser objects, as well as having objects which provide hooks into each Java applet in a page, a JavaScript script can play the role of middleman and cause information generated by user or browser events outside the applet to be passed to any applet. Pretty powerful stuff, overall. The version of JavaScript built into version 2.0 of Netscape Navigator doesn't provide the applet object. Navigator 3.0 supports this feature. Still, this doesn't prevent JavaScript-enabled Web pages from taking advantage of Java applets and even from performing some basic manipulations that would seem to the user to interact with Java applets-even in Navigator 2.0. Basic Java ConceptsBefore you can easily use in your Web pages applets that other people have written, you need to understand several fundamental things about Java. First, Java is compiled. In order to build your own Java applets or to compile source code provided by friendly folk on the Web and in Usenet newsgroups, it is necessary to have a Java compiler. Presently, the Java Developer's Kit is available for SPARC-based hardware running the Solaris operating system and 32-bit Windows platforms (Windows 95 and Windows NT). The compiler and related files and documentation are available at http://www.javasoft.com/ Other groups have ported the Java Developer's Kit to other platforms, such as Linux and the Mac OS. Once the source code for an applet is compiled, it becomes a class file. Class files are not source code and contain objects that can be used in other programs or applets you build. The class file for a Java applet is what is downloaded to the browser and executed when a user loads a page containing the applet. Presently, there are several large archives of freely available applets, which often include source code or even downloadable Java binary files. If you want to use these applets, you can download the source code and compile them yourself or download the actual class files. Information about using the Java compiler is included in the documentation at the Java Web page. The leading archives can be found at this site: http://www.gamelan.com/ and at the Java Web page itself. In order to understand how to go about obtaining and preparing to use existing applets, you are going to prepare the Growing Text applet by Jamie Hall, which you will use for the rest of the chapter. This applet animates any string of text and causes it to grow from very small to very large. The page author can control several different options including color, font, and delay.
I am assuming that you have downloaded the Developer's Kit (which
includes the compiler) from Sun's Java home page and have followed
the installation instructions. The Developer's Kit is available
for several platforms including Windows 95, the Mac OS, and Solaris.
Navigator can run Java applets in its 32-bit Windows version,
its UNIX versions, and the Mac version.
The Growing Text applet can be found on the Web at this site: http://www1.mhv.net/~jamihall/java/GrowingText/GrowingText.html You should download the source code, which looks like Listing 14.1 (remember-this is Java code and not a JavaScript script).
Listing 14.1. The Growing Text applet source code. /*
The demonstration page for this applet looks like Figure 14.1. Figure 14.1 : The Growing Text applet. Once you have the source code, the next step is to compile it. On Windows 95 or NT systems, this involves running the program javac (which is the compiler). For instance, javac GrowingText.java will compile the Java source code file you downloaded. The result of this process should be a file called GrowingText.class. The .class files are Java binaries, and this is the actual executable applet used by the browser. Incorporating Java Applets in HTML with the APPLET Tag
Including a Java applet in an HTML file requires the use of the
APPLET tag. The APPLET
tag specifies the URL of the Java class file for the applet and
tells the browser what size rectangular space to set aside for
use by the applet. This is done using the attributes outlined
in Table 14.1.
The APPLET tag is a container tag. Any text between the opening and closing tags will be displayed by browsers that don't support the APPLET tag (that is, which don't support the beta version of Java). In addition to defining the space in which the APPLET is able to operate, you can also pass parameters-which can be thought of as arguments-to the applet using the PARAM tag. You can include as many PARAM tags-which define name-value pairs for the parameters-as you want between the opening and closing APPLET tags. The PARAM tag takes the form: <PARAM NAME="nameOfParameter" VALUE="valuePassedForParameter"> Using the Growing Text applet, which you compiled in Listing 14.1, you can now build a simple Web page that displays the applet in a 500¥200-pixel rectangle with the words "Java Really Works" as the text used by the applet. As you can see in the source code for the applet (Listing 14.1), several parameters are available for you to set: Takes text, delay, fontName, fontBold, fontItalic, bgColor, For your Web page, you will use a delay of 250 milliseconds and bold type to test the blurring effect. Listing 14.2 shows how to combine the applet into a Web page.
Listing 14.2. Combining the Growing Text applet into a Web page. <HTML>
Figure 14.2 illustrates the effects of the script.
Figure 14.2 : The APPLET tag lets you define the space available to the applet.
The Java applet continues to run until you leave the page or close Netscape. Working with Java in Navigator 2.0Although the applet object is not available in the version of JavaScript implemented in Navigator 2.0, it is still possible to create limited interaction between applets and the browser environment, using JavaScript. For instance, with JavaScript's capability to dynamically generate HTML code, a form in one frame could easily reload a Java applet in another frame, with new parameters. While this is not truly interacting with an applet while it is loaded and executing, it can produce the appearance that the applet is better integrated into a Web application. To demonstrate how dynamically written HTML can be used to change the state of an applet in another frame, let's build a simple testing program for the Growing Text applet. This program should enable the user to enter a string, select options from checkboxes, and fill in fields. When the user clicks on a Test button, the applet should be reloaded in a second frame with the new parameters. Listings 14.3 through 14.5 are the source code for this application.
Listing 14.3. The parent frameset. <!-- SOURCE CODE OF PARENT FRAMESET --> Listings 14.4 and 14.5 are the source code for javatest.html that provides a form to test different parameters of the applet and the code to display it.
Listing 14.4. Source code for the testing form. <!-- SOURCE CODE FOR JAVATEST.htmL -->
Listing 14.5. The code to display the applet. <!-- SOURCE CODE FOR applet.html -->
The results appear in Figure 14.3.
Figure 14.3 : Using JavaScript, you can reload an applet in another frame with new parameters
The file javatest.html makes minimal use of JavaScript. The only place you use JavaScript is in the onClick event handler in the form button, where you reload applet.html into the lower frame to get it to restart the applet with the new parameters. The applets ArrayStarting with Navigator 3.0, Netscape has added the ability for two-way communication between Java and JavaScript. JavaScript 1.1can call public methods in Java applets as well as work with Java objects and properties, and Java applets can call JavaScript functions. In this section we will look at how to call Java applets from JavaScript scripts because this is the key tool to making JavaScript the glue for Java applets. Java applets are reflected into the JavaScipt environment through the applets array-an array of applet objects. Each applet is reflected by a single entry in the array in the order in which it appears in the source code of the HTML document. The applets array is a property of the document object. For instance, the second applet in a document would be accessed with document.applets[1]. Applets can be named using the NAME attribute of the APPLET tag. The applet <APPLET CODE="codeURL" WIDTH=width HEIGHT=height NAME=appletName> could then be accessed with document.appletName. Properties and methods of the applet are then reflected in JavaScript as properties and methods of the corresponding applet object. The following listing includes the Growing Text applet used earlier and adds two HTML form buttons-Start and Stop-which allow the user to start and stop the applet: <HTML> Here, the onClick event handlers in the INPUT tags are used to call the stop() and start() methods of the Growing Text applet. The Java EnvironmentIn addition to being able to access properties and methods in an applet, it is possible to access any Java class or package. However, this requires knowledge of the Java environment and how to use Java classes and packages. More details on how to do this are available on Netscape's Web site. The subject is also covered in detail in the JavaScript Developer's Guide from Sams.net Publishing. From JavaScript to JavaFor many of you, the next step after reading this book will be to look into learning Java. This isn't that outrageous an idea. By learning JavaScript, you have learned the fundamental syntax used throughout Java. You are familiar with how Java commands are built, how to use loops, and how to build expressions. Of course, Java is not the same as JavaScript. Besides being compiled and having access to the same set of objects JavaScript does, there are other significant differences:
The result of these and other more subtle differences between Java and JavaScript is that Java programming can be more complex and require more rigorous debugging and organization than JavaScript scripts.
At the same time, with Java it is possible to write complete standalone
applications and to perform actions not possible with JavaScript.
Currently, JavaScript is limited to products from Netscape, and more recently in the Windows 95 and NT versions of Microsoft's Web browser, Internet Explorer. The most prominent use of JavaScript, which we have discussed throughout this book, is the use of the language for developing client-end applications that are integrated into HTML pages displayed in the Navigator or Internet Explorer browsers. However, Netscape also has implemented JavaScript to use at the server end, much like CGI programming. Using the Netscape product called LiveWire-a server package for developing sophisticated interactive Web applications-it is possible to create CGI-like scripts using JavaScript. This simplifies Web development in many ways because programming at both ends can be done in the same language, rather than requiring the use of JavaScript for the client end of an application and using Perl, C, or another language for the server end. Communicating with Plug-InsIn addition to communication between JavaScript and Java applets, LiveConnect makes it possible for JavaScript to interact with Navigator plug-ins that have been designed to provide LiveConnect support.
Navigator 3.0 includes several plug-ins, such as LiveVideo and
LiveAudio, which provide the necessary support for LiveConnect
and can be accessed from within JavaScript scripts.
The EMBED TagPlug-ins are included in an HTML file with the EMBED tag. The EMBED tag is similar to the IMG tag and the APPLET tag. It allows the embedding of a plug-in file format into a Web page to be downloaded when the page is being rendered by the browser. The tag takes the following attributes:
Similar to frames where the <NOFRAMES> tag provided a mechanism to include alternate HTML code for browsers that don't support frames, the <NOEMBED> tag enables authors to specify HTML code to display in browsers that don't support the <EMBED> tag, and therefore don't support plug-ins. For example, the HTML code <BODY> will display the text "Sorry! You need a plug-ins capable browser." in browsers that don't support the <EMBED> tag. JavaScript includes two objects-mimeTypes and plug-ins-which can be used in scripts to determine if specific plug-ins or MIME types are supported. Both are properties of the navigator object. The plugins ObjectThe plugins object is an array of plugin objects, reflecting each of the available plug-ins in the browser. Each entry in the plugins array has five properties:
You can check for the existence of a particular plug-in by evaluating the plug-in object itself. The code segment if (navigator.plugins["ShockWave"]) outputs the appropriate HTML, based on the existence of the Shockwave plug-in. The mimeTypes ObjectmimeTypes is an array of all the MIME types supported by the browser through any means, including plug-ins and helper applications. Each MIME type is represented by a mimeType object. The array itself is indexed by number or by MIME type names. The mimeType object has three properties:
For instance, for TIFF images, navigator.mimeTypes["image/tiff"].suffixes might have the value "tiff, tif" and navigator.mimeTypes["images/tiff"].description might be equal to "TIFF Image". The embeds ArrayWhereas the plugins array provides information about each plug-in supported by the browser, the embeds array reflects each of the plug-ins used by an EMBED tag in a document. The entries in the array reflect each of the EMBED tags in their order of appearance in the HTML source code. The embeds array is a property of the document object. Each entry in the array has no properties or methods, but provides a mechanism for calling the plug-ins methods from JavaScript. Calling Plug-in Methods from JavaScriptPlug-ins that have been written to interact with JavaScript through LiveConnect make available what are known as native methods. These methods are available to be called by JavaScript scripts as methods of the particular entry in the embeds array. For instance, most versions of Navigator 3.0 come with the LiveVideo plug-in. This plug-in's object is accessible in Java (and therefore in JavaScript). The LiveVideo plug-in documentation indicates that it makes four native methods available to the Java environment:
Using these methods, you can use JavaScript to create a simple control panel for an embedded audio file: <BODY> This file uses JavaScript event handlers to provide control buttons for the video file specified in the EMBED tag. Notice that the plug-in is referred to both by its position in the embeds array as well as by name. SummaryHaving learned to use JavaScript, you looked in this chapter at the relationship between JavaScript and Java and the relationship between JavaScript and plug-ins. We discussed how the applet object allows JavaScript applications to interact with Java applets. You also learned how to incorporate existing Java applets into HTML pages and to use JavaScript to pass custom parameters to the applets you are using. Finally, you took a look at how to make the move from JavaScript scripting to Java programming and considered the future development of JavaScript. A discussion of the mimeTypes and plugins objects, plus the embeds array, provided an introduction to interacting with plug-ins from JavaScript. Q&A
|
Use of this site is subject certain Terms & Conditions. Copyright (c) 1996-1999 EarthWeb, Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Please read our privacy policy for details. |