home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 23.1 Applet1.htm—Controlling an Applet Using Its Name


<HTML>
<HEAD>
<TITLE>JavaScript Control of a Java Applet I</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<H1>JavaScript Control of a Java Applet I</H1>
<HR>
<APPLET NAME=“Counter” CODE=“Counter.class” WIDTH=200 HEIGHT=100></APPLET>
<FORM>
<INPUT TYPE=BUTTON VALUE=“Add 1” NAME=“AddButton”
       onClick=“document.Counter.increment()”>
<INPUT TYPE=BUTTON VALUE=“Subtract 1” NAME=“SubtractButton”
       onClick=“document.Counter.decrement()”>
</FORM>
</BODY>
</HTML>

As shown in Listing 23.1, the Java applet’s increment() and decrement() methods are called through the JavaScript attached to the onClick event of the two HTML forms buttons. The applet’s methods are accessed by referring to the Java applet object, which is included in the object hierarchy under the document object.

What if the applet isn’t named? You can still access Java applets in your HTML documents, even if you don’t specify a NAME attribute in the <APPLET> tag. This is done using JavaScript’s object arrays, in this case the applets array associated with the applet object. Listing 23.2 shows the same example as Listing 23.1, demonstrating how to reference the Java applet without using a name.

  See “JavaScript Object Arrays,” p. 470.

Listing 23.2 Applet2.htm—Controlling an Applet Without Using Its Name


<HTML>
<HEAD>
<TITLE>JavaScript Control of a Java Applet I</TITLE>
</HEAD>
<BODY BGCOLOR=#FFFFFF>
<H1>JavaScript Control of a Java Applet I</H1>
<HR>
<APPLET CODE=“Counter.class” WIDTH=200 HEIGHT=100></APPLET>
<FORM>
<INPUT TYPE=BUTTON VALUE=“Add 1?” NAME=“AddButton”
       onClick=“document.applets[0].increment()”>
<INPUT TYPE=BUTTON VALUE=“Subtract 1” NAME=“SubtractButton”
       onClick=“document.applets[0].decrement()”>
</FORM>
</BODY>
</HTML>

Because the Java applet is the only one in the HTML document, it is referenced through the first position in the applet object array, as applets[0].

Plug-in Content from the <EMBED> Tag

Accessing plug-in objects, properties, and methods included in an HTML document through the <EMBED> tag is done in the same manner as with Java applets, which was discussed in the preceding section. Consider, for example, the following tag included as the first embed in your HTML document:

<EMBED NAME=“MyPlug” SRC=“MyMovie.dcr” WIDTH=300 HEIGHT=200></EMBED>

If the plug-in that was called to support this content type exposed object and methods to the Web browser and to JavaScript, you could access them in any of three ways. If the plug-in had an initialize() method you wanted to call, for example, you could do so by using one of the following:

  document.MyPlug.initialize()
  document.embeds[0].initialize()
  document.embeds[“MyPlug”].initialize()

The last syntax shown is known as an associative array and was discussed in Chapter 19. That chapter also discussed when each of the syntaxes in this list is most appropriate.

Accessing <OBJECT> Tag Included Objects

By comparison with the <APPLET> and <EMBED> tags, objects included with the <OBJECT> tag are a bit easier to reference with JavaScript. That is because these objects are attached to the window object, and so can be referenced directly. If, for example, an object were included in your HTML document using the following:

<OBJECT NAME=“MyControl”…></OBJECT>

And if it had an initialize() method, for example, it could be called from JavaScript by using just MyControl.initialize().

Netscape’s LiveConnect

Since its introduction, Netscape has introduced many new technologies into its Navigator Web browser. In addition to HTML extensions and multimedia capabilities such as LiveAudio, LiveVideo, and Live3D, three technologies in particular were introduced, or first became widely used, with Netscape Navigator. These are Web browser scripting with JavaScript, Java applet support, and Web browser plug-ins.

Until the release of Netscape Navigator 3, these technologies suffered from the handicap of being completely separate applications within a Web browser. JavaScripts, Java applets, and Navigator plug-ins ran within Navigator on their own, without the capability to interact. However, with Navigator 3 and now 4, Netscape has introduced its LiveConnect technology, enabling its three systems to interact.

Figure 23.1 shows how LiveConnect works within the Netscape Navigator runtime environment. JavaScripts can call Java methods and plug-in functions. Java applets can call both JavaScript and plug-in functions. Plug-ins can call Java methods and, through Java, call JavaScript functions. The objects and properties of each LiveConnect-compatible Java applet and plug-in are available to be manipulated through JavaScripts, applets, and other plug-ins.


FIGURE 23.1  Netscape’s LiveConnect technology enables JavaScript, Java, and plug-ins to work together within Netscape Navigator.

More extensive information on Netscape’s LiveConnect technology is available on its Web site at http://developer.netscape.com/docs/technote/javascript/liveconnect/liveconnect_rh.html.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.