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


For this to work, therefore, the Java methods need to be defined as public methods. When called, each method takes the parameter supplied and changes the characteristics of the applet (see Figure 23.2). As an example, the setText method is defined as a public method, as shown here:

public void setText(int which,String text) {
   bgChange = true;
   thoughts.theThoughts[which] = text;
   thoughts.Reset();
}


FIGURE 23.2  JavaScript is LiveConnect’s link between the HTML form user input and Java applets.


NOTE:  As mentioned a little earlier in this chapter, Internet Explorer achieves the same JavaScript to Java communication by using Microsoft’s own ActiveX technology rather than Netscape’s LiveConnect. Figure 23.3 demonstrates this, showing the same Netscape demo page shown in Figure 23.2 displayed instead in Internet Explorer.

However, this example also shows that Microsoft and Netscape don’t quite have compatible versions of JavaScript. Although Internet Explorer enables you to change most of the parameters of the Fader applet, it does not support the JavaScript 1.2 font object used to change the display font. (If you look at this Web page with Internet Explorer, you’ll get a few error dialogs, such as that shown in Figure 23.4; as long as you choose to keep running scripts, you’ll be able to change everything but the display font for the applet.)



FIGURE 23.3  Microsoft accomplishes the same goal of JavaScript to Java communication with different means—but the same HTML and JavaScript will work with both browsers.


FIGURE 23.4  Internet Explorer and Navigator do not yet support completely compatible versions of JavaScript.

Java to JavaScript Communication

The first step in enabling your Java applets to access JavaScript properties is to import the javascript package into your Java applet, as shown here:

import netscape.javascript.*

This enables the Java applet to access JavaScript properties through the JSObject class. However, the author of the HTML document must still enable access to his JavaScript by including the MAYSCRIPT attribute in the <APPLET> tag used to include the Java applet. If the Fader used in the last example needed to access JavaScript, for example, the <APPLET> tag would look like the following:

<APPLET CODE=“Fade.class” NAME=“Fader” MAYSCRIPT WIDTH=400 HEIGHT=100>
<PARAM NAME=“text1” VALUE=“Look at this text carefully!”>
<PARAM NAME=“url1”  VALUE=“<http://www.netscape.com>”>
<PARAM NAME=“font1” VALUE=“Helvetica,PLAIN,36”>
</APPLET>

If these two conditions have been satisfied, accessing JavaScript objects or methods is a two-step process, as follows:

1.  Get a handle for the Navigator window containing the JavaScript objects or methods you want to access. This can be done using the getWindow() method of the netscape.javascript.JSObject package:
// winhan is a variable of type JSObject
public void initialize() {
   winhan = JSObject.getWindow(this);
}
2.  To access JavaScript objects and properties, do the following:
Use the getMember() method in the netscape.javascript.JSObject package to access each JavaScript object in turn. To access the JavaScript object document.testForm using the window handle found in step 1, you could do the following:
public void accessForm(JSObject winhan) {
   JSObject myDoc = (JSObject) winhan.getMember(“document”);
   JSObject myForm = (JSObject) myDoc.getmember(“testForm”);
}
3.  To call a JavaScript method:
Use either the call() or eval() method of the netscape.javascript.JSObject class. The syntax for the two commands (using the window handle found in step 1) is as follows:
winhan.call(“methodName”,arguments)
winhan.eval(“expression”)

In the former, methodName is the name of the JavaScript method, and arguments is an array of arguments to be passed on to the JavaScript method. In the latter, expression is a JavaScript expression that, when evaluated, has a value that is the name of a JavaScript method.

Similar to JavaScript to Java communication, Java to JavaScript communication can be useful when you don’t want to re-create an input or output interface within your Java applet. You might create a Java applet that makes use of Java’s network and Internet capabilities to access data from a database server on some other machine. Rather than using Java to display this data, you can access JavaScript objects to display it within a conventional HTML form.

JavaScript and Plug-Ins

JavaScript can be used with the client to determine what plug-ins the client has installed and what MIME types are supported. This is done through the navigator object, through two of its properties: plugins and mimeTypes. JavaScripts can also be used to call plug-in functions.


NOTE:  Internet Explorer also supports the navigator object, so the techniques in this section should work for both Netscape Navigator and Microsoft Internet Explorer. Be aware, however, that Internet Explorer does not support all Navigator plug-ins. n

By determining at the client whether a particular plug-in is installed or MIME-type supported, you can write scripts that generate content dynamically. If a particular plug-in is installed, the appropriate plug-in data can be displayed; otherwise, some alternative image or text can be shown.


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.