LiveConnect communication

LiveConnect enables communication between JavaScript and Java applets in a page and between JavaScript and plug-ins loaded on a page.

  • Use JavaScript to access Java variables, methods, classes, and packages directly.
  • Control Java applets or plug-ins with JavaScript.
  • Use Java code to access JavaScript methods and properties.

    This documentation tells you how to use the LiveConnect feature of Netscape Navigator, and it assumes that you are familiar with Java programming. This documentation is divided into the following sections:


    Enabling LiveConnect

    LiveConnect is enabled by default in Navigator 3.0 Beta 4 and later. To confirm that it is enabled, choose Network Preferences from the Options menu, then choose the Languages tab.

  • Make sure Enable Java is checked.
  • Make sure Enable JavaScript is checked.

    To disable either Java or JavaScript, uncheck the checkboxes.


    The Java Console

    The Java Console is a Navigator window that displays Java messages. When you use the class variables out or err in java.lang.System to output a message, the message appears in the Console.

    For example, the following Java code displays the message "Hello, world!" in the Java Console:

    public void init() {
       System.out.println("Hello, world!");
    }
    

    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.

    To display the Java Console, choose Show Java Console from the Options menu.


    About the Netscape packages

    Navigator 3.0 contains a 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.

    The new 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:

  • netscape.javascript implements the JSObject class to let your Java applet access JavaScript methods and properties. It also implements JSException to throw an exception when JavaScript code returns an error.
  • netscape.plugin implements the Plugin class to enable JavaScript and plug-in communication. Compile your plug-in with this class to allow applets and JavaScript code to manipulate the plug-in.

    These packages are documented in the Netscape packages reference.

    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.

    These packages are not documented because they are implemented the same as the original Sun packages.

    Using the Netscape packages

    The java_30 file is delivered in the Program\java\classes directory beneath the Navigator directory. To access the packages in java_30, place the file in the classpath of the JDK compiler in either of the following ways:

  • Create a CLASSPATH environment variable to specify the paths and names of java_30 and classes.zip.
  • Specify the location of java_30 and classes.zip when you compile by using the -classpath command line parameter.

    For example, 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\classesjava_30

    See the Sun JDK documentation for more information about classpath.


    JavaScript to Java communication

    LiveConnect provides three ways for JavaScript to communicate with Java:

  • JavaScript can call Java methods directly.
  • JavaScript can control Java applets.
  • JavaScript can control Java plug-ins.

    Accessing Java directly

    When LiveConnect is enabled, JavaScript can make direct calls to Java methods. For example, you can call 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 reference Java objects in JavaScript, with the name of the Packages object optionally prepended:

    [Packages.]packageName.className.methodName

    The 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.System
    System.err.println("Greetings from JavaScript")
    

    In this example, the variable System refers to the class java.lang.System. The static variable err in System invokes the println method. Because println expects a java.lang.String argument, the JavaScript string is converted to a java.lang.String automatically.

    You can even use Java class constructors in JavaScript. For example

    var mydate = new java.util.Date()

    Controlling applets

    You can use JavaScript to control the behavior of a Java applet without knowing much about the internal construction of the applet. All public variables, methods, and properties of an applet are available for JavaScript access. For example, you can use buttons on an HTML form to start and stop a Java applet that appears elsewhere in the document.

    Referencing applets

    Each applet in a document is reflected in JavaScript by document.appletName, where appletName is the value of the NAME attribute of the APPLET tag. For example, the following HTML launches an applet called "mojaApplet":

    <APPLET CODE=Mojatest.class NAME=mojaApplet WIDTH=60 HEIGHT=30> <PARAM NAME=label VALUE=Mojatest> <PARAM NAME=debug VALUE=86> </APPLET>

    You can reference this applet in JavaScript in either of the following ways:

    document.mojaApplet document.applets["mojaApplet"]

    You can also reference this applet through the applets array. For example, if it is the first applet in the document:

    document.applets[0]

    The applets array has a length property, document.applets.length, that indicates the number of applets in the document.

    Accessing applets

    All public variables declared in an applet, 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.

    For example, the following applet displays text that flashes in different colors (click here to display the source for this applet). Two pushbuttons start and stop this applet, and another pushbutton changes the flashing text that it displays:


    Flashing text applet

    Enter new text for the flashing display:

    Click the button to change the display:


    This applet uses the public methods start and stop to begin and halt its execution. The start method in this application is defined in the Java source as follows:

    public void start() {
       if (myThread == null) {
          myThread = new Thread(this);
          myThread.start();
       }
    }
    

    The public keyword indicates that the start method is available outside its class; in this case, it is available to JavaScript. The stop method for this application is similarly defined.

    In the HTML form, the onClick event handler of the pushbuttons calls these methods as follows:

    <INPUT TYPE="button" Value="Start" NAME="startButton" onClick="document.colorApp.start()"> <INPUT TYPE="button" Value="Stop" NAME="stopButton" onClick="document.colorApp.stop()"> Similarly, the public method setString in the applet specifies the text for the flashing string that appears. The following JavaScript event handler lets a user change the "Hello, world!" string that the applet initially displays:

    <INPUT TYPE="button" VALUE="Change Text" onClick="document.colorApp.setString(document.colorText.textBox.value)">

    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 plug-ins

    Each plug-in in a document is reflected in JavaScript as an element in the embeds array. For example, the following HTML includes an AVI plug-in in a document:

    <EMBED SRC=myavi.avi WIDTH=320 HEIGHT=200> If this HTML defines the first plug-in in a document, you can use the following code to access it:

    document.embeds[0]

    If the plug-in is associated with the Java class netscape.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 with JavaScript for more information about plug-ins.

    The Plug-in Developer's Guide contains information on:


    Java to JavaScript communication

    To access JavaScript methods, properties, and data structures from your Java applet, import the Netscape javascript package as shown in the following example:

    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. For example, to allow the musicPicker.class applet access to JavaScript on your page, specify the following:

    <APPLET CODE = "musicPicker.class" WIDTH=200 HEIGHT=35 NAME="musicApp" MAYSCRIPT>

    Accessing JavaScript when the MAYSCRIPT attribute is not specified results in an exception.

    Getting a handle for the JavaScript window

    Before you can access JavaScript, you must get a handle for the Navigator window. Use the getWindow method in the class netscape.javascript.JSObject to get a window handle.

    For example, if win is a previously-declared variable of type JSObject, the following code assigns a window handle to win:

    public void init() {
       win = JSObject.getWindow(this);
    }
    

    Accessing JavaScript objects and properties

    The getMember method in the class netscape.javascript.JSObject enables you to 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() { win = JSObject.getWindow(this); JSObject doc = (JSObject) win.getMember("document"); JSObject myForm = (JSObject) doc.getMember("testForm"); }

    Notice that JavaScript objects appear as instances of the class netscape.javascript.JSObject in Java. Values passed between Java to JavaScript are converted as described in the JSObject Package documentation.

    If the JavaScript object document.testForm.jazz is a checkbox, the following 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 call and eval methods in the class netscape.javascript.JSObject enable you to call JavaScript methods. Use getWindow to get a handle for the JavaScript window, then use call or eval to access a JavaScript method.

    Use either of the following syntaxes to call JavaScript methods:

    1. JSObject.getWindow().call("methodName", arguments)
    2. JSObject.getWindow().eval("expression")
    

    In the first form of this syntax, methodName is the name of the JavaScript method you want to call, and arguments is an array of arguments to pass to the JavaScript method. In the second form of the syntax, expression is a JavaScript expression that evaluates to a JavaScript method.

    For example, the following code uses call 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) { win.call("alert(\"Hello world!\");"); return true; }

    Similarly, the following code uses eval to call the JavaScript alert method:

    public void init() { JSObject win = JSObject.getWindow(this); } public boolean mouseUp(Event e, int x, int y) { win.eval("alert(\"Hello world!\");"); return true; }


    Last modified Wed Dec 31 19:00:00 1969