Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

Passing Values Between Java and JavaScript

JSObject allows Java to manipulate objects that are defined in JavaScript. Values passed from Java to JavaScript are converted as follows:

  • JSObject is converted to the original JavaScript object.
  • Any other Java object is converted to a JavaScript wrapper, which can be used to access methods and fields of the java object. Converting this wrapper to a string will call the toString() method on the original object; converting it to a number will call the floatValue() method if possible, and will fail otherwise. Converting it to a Boolean value will try to call the booleanValue() method in the same way.
  • Java arrays are wrapped with a JavaScript object that understands array.length and array[index]—the Array object. Therefore, a Java array in JavaScript is actually an instance of the Array object, thus featuring the same properties and methods of any other instance of this object.
  • A Java boolean is converted to a JavaScript Boolean value.
  • Java byte, char, short, int, long, float, and double are converted to JavaScript numbers. Since JavaScript is loosely typed, these data types are not distinguished in JavaScript as they are in Java.

Values passed from JavaScript to Java are converted as follows:

  • Objects which are wrappers around java objects are unwrapped. This conversion is the opposite of the second one in the Java-to-JavaScript conversion listing.
  • Other objects, which were not originally Java objects, are wrapped with a JSObject.
  • Strings, numbers, and Boolean values are converted to String, Float, and Boolean objects, respectively.

    All JavaScript values show up as some kind of java.lang.Object in Java. In order to make use of them, you will have to cast them to subclasses of java.lang.Object, using the regular casting technique.

    Getting a Handle for the Browser Window

    Before you can access JavaScript, you must get a handle for the browser window. Use the getWindow() method of the JSObject class to do so.

    The following Java class gets a handle for the browser window containing the applet:

    import netscape.javascript.JSObject;
    import java.applet.Applet;
    
    public class js extends Applet {
     JSObject 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. You should invoke this function as a method of a JavaScript object reflected in Java (the JavaScript object is wrapped with a JSObject). The getMember() method retrieved a named property of a JavaScript object, thus returning a value equivalent to this.name in JavaScript. This method receives the name of the desired property in the form of a string. The following Java method provides you the value of a check box in an HTML document (provided that the applet is loaded from the same HTML document):

    public void init() {
      win = JSObject.getWindow(this);
      JSObject doc = (JSObject) win.getMember("document");
      JSObject myForm = (JSObject) doc.getMember("myForm");
      JSObject check = (JSObject) myForm.getMember("myBox");
      Boolean isChecked = (Boolean) check.getMember("checked");
    }
    

    Since data type casting is often very confusing, take special precautions when using such routines.

    Don’t worry if you don’t completely understand this topic. As you will see later, you will probably never need to use it, because it is much easier to hand values from JavaScript to Java rather than the opposite way.

    Calling JavaScript Methods

    The eval() method in the class netscape.javascript.JSObject enables you to execute any valid JavaScript statement within a Java code. Use getWindow() to get a handle for the JavaScript window (the window containing the desired JavaScript functions or methods), then use eval() to execute a JavaScript statement. The eval() method is mostly used to invoke JavaScript methods, using the following syntax:

    JSObject.getWindow().eval("expression")
    

    Be aware that if you want to hand an argument to a JavaScript method in the form of a Java data structure (e.g., a String or an int), the argument should first be evaluated to its literal value in the Java applet. For example, you can use the following Java method to invoke the JavaScript’s alert() method:

    public void JSalert(String str) {
      win.eval("alert(\"" + str + "\")");
    }
    

    Example 31-1 shows how to invoke a JavaScript method and a JavaScript function using Java.

    <HTML>
    <HEAD>
    <TITLE>Invoking a JavaScript method from a Java Applet</TITLE>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
    
    function myAlert(str) {
      window.alert(str)
    }
    
    // -->
    </SCRIPT>
    </HEAD>
    <BODY>
    <APPLET CODE="callMe" NAME="callMe" MAYSCRIPT>
    </APPLET>
    </BODY>
    </HTML>
    

    Example 31-1 (ex31-1.htm). A simple HTML document with a Java applet and a deferred JavaScript function.

    Here’s the source code for the callMe class in Example 31-1:

    import netscape.javascript.JSObject;
    import java.applet.Applet;
    
    public class callMe extends Applet {
      JSObject win;
    
      public void init() {
    win = JSObject.getWindow(this);
    makeAlert1();
    makeAlert2();
      }
    
      private void makeAlert1() {
    win.eval("alert(\"LiveConnect is cool.\")");
      }
    
      private void makeAlert2() {
    win.eval("myAlert(\"LiveConnect is cool.\")");
      }
    }
    

    Example 31-1a (callMe.java). The source code for the callMe class.




With any suggestions or questions please feel free to contact us