Документ взят из кэша поисковой машины. Адрес
оригинального документа
: http://www.sao.ru/cats/~satr/JS/Guide/livecon.htm
Дата изменения: Fri Dec 12 11:19:04 1997 Дата индексирования: Tue Oct 2 07:10:36 2012 Кодировка: Поисковые слова: aircraft |
For the HTML syntax required to add applets and plug-ins to your web page, see the Applet
and Plugin
objects in the JavaScript Reference.
For information on using LiveConnect with server-side JavaScript, see Writing Server-Side JavaScript Applications.
For additional information on using LiveConnect, see the JavaScript technical notes.
Enabling LiveConnect
LiveConnect is enabled by default in Navigator 3.0 and later. For LiveConnect to work, both Java and JavaScript must be enabled. To confirm they are enabled, choose Preferences from the Edit and display the Advanced section.
out
or err
in java.lang.System
to output a message, the message appears in the Console. To display the Java Console, choose Java Console from the Communicator menu.
You can use the Java Console to present messages to users, or to trace the values of variables at different places in a program's execution.
For example, the following Java code displays the message "Hello, world!" in the Java Console:
public void init() {You can use the Java Console to present messages to users, or to trace the values of variables at different places in a program's execution. Note that most users probably do not display the Java Console.
System.out.println("Hello, world!")
}
java_30
file that includes the following Java packages:
netscape
packages to enable JavaScript and Java communication.
java
and sun
packages to provide security enhancements for LiveConnect.
java
and sun
packages replace packages in the Sun 1.0.2 Java Development Kit (JDK) classes.zip
. These packages have been tested by Sun, and similar security enhancements will be implemented in future releases of the Sun JDK. Use these packages until the Sun JDK provides these security enhancements.
The file java_30
contains the following netscape
packages:
In addition, java_30
contains some other netscape
packages:
netscape.applet
is a replacement for the Sun JDK package sun.applet
.
netscape.net
is a replacement for the Sun JDK package sun.net
.
java_30
, place the file in the CLASSPATH
of the JDK compiler in either of the following ways:
CLASSPATH
environment variable to specify the paths and names of java_30
and classes.zip
.
java_30
when you compile by using the -classpath
command line parameter.
java_30
file is delivered in the Program\Java\classes
directory beneath the Navigator directory. You can specify an environment variable in Windows NT by double-clicking the System icon in the Control Panel and creating a user environment variable called CLASSPATH
with a value similar to the following:
D:\JDK\java\lib\classes.zip;D:\Navigator\Program\java\classes\java_30See the Sun JDK documentation for more information about
CLASSPATH
.
System.out.println
to display a message on the Java Console.
In JavaScript, Java packages and classes are properties of the Packages
object. Use Java syntax to refer to Java objects in JavaScript, with the name of the Packages
object optionally prepended:
[Packages.]packageName.className.methodNameThe name
Packages
is optional for java
, sun
, and netscape
packages; in code, java
, sun
, and netscape
are aliases for Packages.java
, Packages.sun
, and Packages.netscape
. For example, you can refer to the Java class java.lang.System
as either Packages.java.lang.System
or as java.lang.System
in your code. The name Packages
is required for other packages.
Access fields and methods in a class with the same syntax that you use in Java. For example, the following JavaScript code prints a message to the Java Console:
var System = java.lang.SystemThe first line in this example makes the JavaScript variable
System.err.println("Greetings from JavaScript")
System
refer to the class java.lang.System
. The second line invokes the println
method of the static variable err
in the Java System
class. Because println
expects a java.lang.String
argument, the JavaScript string is automatically converted to a java.lang.String
.
You can even use Java class constructors in JavaScript. For example, the following JavaScript code creates a Java Date
object and prints it to the Java Console.
var mydate = new java.util.Date()
System.out.println(myDate)
document.appletName
, where appletName
is the value of the NAME
attribute of the <APPLET>
tag. The applets
array also contains all the applets in a page; you can refer to elements of the array through the applet name (as in an associative array) or by the ordinal number of the applet on the page (starting from zero).
For example, consider the basic "Hello World" applet in Java:
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {The following HTML runs and displays the applet, and names it "HelloWorld" (with the
public void paint(Graphics g) {
g.drawString("Hello world!", 50, 25);
}
}
NAME
attribute):
<APPLET CODE="HelloWorld.class" NAME="HelloWorld" WIDTH=150 HEIGHT=25>If this is the first applet in the document (topmost on the page), you can refer to it in JavaScript in any of the following ways:
</APPLET>
document.HelloWorldThe
document.applets["HelloWorld"]
document.applets[0]
applets
array has a length
property, document.applets.length
, that indicates the number of applets in the document.
All public variables declared in an applet, and its ancestor classes and packages are available in JavaScript. Static methods and properties declared in an applet are available to JavaScript as methods and properties of the Applet
object. You can get and set property values, and you can call methods that return string, numeric, and boolean values.
import java.applet.Applet;
import java.awt.Graphics;
public class HelloWorld extends Applet {
String myString;
public void init() {Making the message string a variable allows you to modify it from JavaScript. Now modify the HTML file as follows: The HTML file now looks like this:
myString = new String("Hello, world!");
}
public void paint(Graphics g) {
g.drawString(myString, 25, 20);
}
public void setString(String aString) {
myString = aString;
repaint();
}
}
<APPLET CODE="HelloWorld1.class" NAME="Hello" WIDTH=150 HEIGHT=25>
</APPLET>
<FORM NAME="form1">When you compile the HelloWorld applet, and load the HTML page into Navigator, you initially see "Hello, World!" displayed in the gray applet panel. However, you can now change it by entering text in the text field and clicking on the button. This demonstrates controlling an applet from JavaScript.
<INPUT TYPE="button" VALUE="Set String"
onClick="document.HelloWorld.setString(document.form1.str.value)">
<BR>
<INPUT TYPE="text" SIZE="20" NAME="str">
</FORM>
Figure 5.1 Flashing text applet
<APPLET CODE="colors.class" WIDTH=500 HEIGHT=60 NAME="colorApp">
</APPLET><FORM NAME=colorText>
<P>Enter new text for the flashing display:<INPUT TYPE="text"
NAME="textBox"
LENGTH=50><P>Click the button to change the display:
<INPUT TYPE="button"
VALUE="Change Text"
onClick="document.colorApp.setString(document.colorText.textBox.value)"></FORM>
This applet uses the public method setString
to specify the text for the flashing string that appears. In the HTML form, the onClick
event handler of the button lets a user change the "Hello, world!" string that the applet initially displays by calling the setString
method.
In this code, colorText
is the name of the HTML form and textBox
is the name of the text field. The event handler passes the value that a user enters in the text field to the setString
method in the Java applet.
Controlling Java Plug-ins
Each plug-in in a document is reflected in JavaScript as an element in the embeds
array. For example, the following HTML code includes an AVI plug-in in a document:
<EMBED SRC=myavi.avi NAME="myEmbed" WIDTH=320 HEIGHT=200>
If this HTML defines the first plug-in in a document, you can access it in any of the following ways:
document.embeds[0]
If the plug-in is associated with the Java class
document.embeds["myEmbed"]
document.myEmbednetscape.plugin.Plugin
, you can access its static variables and methods the way you access an applet's variables and methods.
The embeds
array has a length
property, document.embeds.length
, that indicates the number of plug-ins embedded in the document. See "Determining Installed Plug-ins" for more information about plug-ins.
The Plug-in Guide contains information on:
java.lang.Object
. To use them, you generally will have to cast them to the appropriate subclass of Object
, for example:
(String) window.getMember("name");
(JSObject) window.getMember("document");.
javascript
package:
import netscape.javascript.*The package
netscape.javascript
defines the JSObject
class and the JSException
exception object.
The author of an HTML page must permit an applet to access JavaScript by specifying the MAYSCRIPT
attribute of the <APPLET>
tag. This prevents an applet from accessing JavaScript on a page without the knowledge of the page author. Attempting to access JavaScript from an applet that does not have the MAYSCRIPT
attribute generates an exception. The MAYSCRIPT
tag is needed only for Java to access JavaScript; it is not needed for JavaScript to access Java.
getWindow
method in the class netscape.javascript.JSObject
to get a window handle, passing it the Applet
object.
For example, if win
is a previously-declared variable of type JSObject
, the following Java code assigns a window handle to win
:
public class myApplet extends Applet {
public void init() {
JSObject win = JSObject.getWindow(this);
}
}
getMember
method in the class netscape.javascript.JSObject
lets you access JavaScript objects and properties. Call getWindow
to get a handle for the JavaScript window, then call getMember
to access each JavaScript object in a containership path in turn.
For example, the following Java code allows you to access the JavaScript object document.testForm
through the variable myForm
:
public void init() {Note that you could use the following lines in place of
win = JSObject.getWindow(this);
myForm=win.eval("document.testForm")
}
myForm=win.eval("document.testForm")
:
JSObject doc = (JSObject) win.getMember("document");Notice that JavaScript objects appear as instances of the class
JSObject myForm = (JSObject) doc.getMember("testForm");
netscape.javascript.JSObject
in Java. Values passed between Java and JavaScript are converted as described in the JavaScript Reference.
If the JavaScript object document.testForm.jazz
is a checkbox, the following Java code allows you to access its checked
property:
public void init() {
win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject myForm = (JSObject) doc.getMember("testForm");
JSObject check = (JSObject) myForm.getMember("jazz");
Boolean isChecked = (Boolean) check.getMember("checked");
} Calling JavaScript Methods
The eval
method in the class netscape.javascript.JSObject
let you evaluate an arbitrary JavaScript expression. Use getWindow
to get a handle for the JavaScript window, then use eval
to access a JavaScript method.
Use the following syntax to call JavaScript methods:
JSObject.getWindow().eval("expression")
expression
is a JavaScript expression that evaluates to a JavaScript method call.
For example, the following Java code uses eval
to call the JavaScript alert
method when a MouseUp event occurs:
public void init() {
JSObject win = JSObject.getWindow(this);
}public boolean mouseUp(Event e, int x, int y) {
Another way to call JavaScript methods is with the
win.eval("alert(\"Hello world!\");");
return true;
}call
method of JSObject
. Use the following to call a JavaScript method from Java when you want to pass Java objects as arguments:
JSObject.call(methodName, argArray)
where argArray
is an Array of Java objects used to pass arguments to the JavaScript method.
If you want to pass primitive values to a JavaScript method, you must use the Java object wrappers (such as Integer
, Float
, and Boolean
), and then populate an Array
with such objects.
Example: Hello World
Returning to the HelloWorld example, modify the paint
method in the Java code so that it calls the JavaScript alert
method (with the message "Painting!") as follows:
public void paint(Graphics g) {
Then add the
g.drawString(myString, 25, 20);
JSObject win = JSObject.getWindow(this);
String args[] = {"Painting!"};
win.call("alert", args);
}MAYSCRIPT
attribute to the <APPLET>
tag in the HTML page, recompile the applet, and try it. Each time the applet is painted (when it is initialized, when you enter a new text value, and when the page is reloaded) a JavaScript alert box is displayed. This is a simple illustration of calling JavaScript from Java.
This same effect could be achieved with the following:
public void paint(Graphics g) {
g.drawString(myString, 25, 20);
JSObject win = JSObject.getWindow(this);
win.eval("alert(`Painting')");
}
NOTE: You may have to reload the HTML page by choosing Open Page from the File menu instead of clicking the Reload button, to ensure that the applet is re- initialized.
<HEAD>
of the HTML page with the HelloWorld applet:
<SCRIPT>This simple function displays an alert dialog box containing the name and version of the client software being used. Then modify the
function test() {
alert("You are using " + navigator.appName + " " +
navigator.appVersion)
}
</SCRIPT>
init
method in your Java code similarly to how you modified paint
:
public void init() {Notice that
myString = new String("Hello, world!")
JSObject win = JSObject.getWindow(this)
String args2[] = {""}
win.call("test", args2)
}
args2
is declared as an array with no elements, even though the method does not take any arguments. When you recompile the applet and reload the HTML page (and reinitialize the applet), a JavaScript alert dialog box will display the version of Navigator you are running. This is a simple illustration of calling a user-defined function from Java.
JSObject
is converted to the original JavaScript object.
Last Updated: 11/26/97 09:25:37