|
First, we declare a variable (which is an object) of type JSObject. The init() method associates this variable with the browser window using the getWindow() method in netscape.javascript.JSObject. It then invokes the other methods in order. The method makeAlert1() generates a JavaScript alert dialog box by calling the window object’s alert() method with a literal argument. The second method, makeAlert2(), does exactly the same by calling a user-defined JavaScript function in the HTML document. Both methods use the eval() method in netscape.javascript.JSObject. Other methods in the JSObject classHere is a complete listing of all the methods in the netscape.javascript.JSObject class, as explained in Netscape’s documentation.
JavaScript ExceptionThe netscape.javascript.JSException class enables you to catch JavaScript exceptions. A JavaScript exception occurs as a result of a JavaScript error caused by a Java applet. In order to execute Java code when a JavaScript exception occurs, you must catch the exception using the following syntax:
try { Java code involving JSObject methods } catch (JSException e) { Java code to execute when a JavaScript exception occurs }
Bear in mind that you should either import netscape.javascript.JSException or use this complete specification instead of JSException. For more information regarding the netscape.javascript.JSException class, take a look at <http://home.netscape.com/ eng/mozilla/3.0/handbook/javascript/packages/netscape.javascript.JSException.html>. JavaScript to Java CommunicationLiveConnect provides three ways for JavaScript to communicate with Java:
You may have noticed that we did not introduce many examples and detailed explanations in the previous section. As a JavaScript scripter, you do not need to know how to write a Java applet (though it would be very useful). However, in the way Java programmers need to know how to access JavaScript from Java, you should know how to access existing applets from JavaScript. In this section we discuss each of the three techniques in depth, including some useful examples. Accessing Java DirectlyWhen 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. So the general syntax for invoking a Java method in a JavaScript script is as follows:
[Packages.]packageName.className.methodName
The name of the Packages object is optional for java, sun, and netscape packages. For example, the System class can be referenced in one of the following ways:
Packages.java.lang.System java.lang.System
Access properties and methods in a class in the same way you would access them in Java. The following script segment, for instance, displays a message in the Java Console:
var System = java.lang.System System.err.println("This is JavaScript invoking a Java method!")
Because println() expects a String (java.lang.String) argument, the JavaScript interpreter automatically converts the JavaScript string to a Java String data type. Example 31-2 shows how to extract the dimensions and resolution of the user’s screen and assign them to JavaScript variables.
<HTML> <HEAD> <TITLE>Accessing Java directly</TITLE> <SCRIPT LANGUAGE="JavaScript"> <!-- var toolkit = java.awt.Toolkit.getDefaultToolkit() var screenSize = toolkit.getScreenSize() var width = screenSize.width var height = screenSize.height var dpi = toolkit.getScreenResolution() alert("Your screen resolution is " + width + " x " + height + " at " + dpi + " dpi!") // --> </SCRIPT> </HEAD> <BODY> </BODY> </HTML>
Example 31-2 (ex31-2.htm). Using LiveConnect, you can find the exact dimensions of the user’s screen, and even its resolution (dots per inch). Controlling Java AppletsThe idea of enabling JavaScript scripts to control Java applets is to allow scripters to control the behavior of an applet without having to know much Java. However, in order to understand how to create an applet suitable for JavaScript access, or to control an existing Java applet with no documentation, you must know some Java basics.
|
|||||||||||||||||||||||
With any suggestions or questions please feel free to contact us |