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

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 class

Here is a complete listing of all the methods in the netscape.javascript.JSObject class, as explained in Netscape’s documentation.

  • public Object getMember(String name)—Retrieves a named member of a JavaScript object. Equivalent to “this.name” in JavaScript.
  • public Object getSlot(int index)—Retrieves an indexed member of a JavaScript object. Equivalent to “this[index]” in JavaScript.
  • public void setMember(String name, Object value)—Sets a named member of a JavaScript object. Equivalent to “this.name = value” in JavaScript.
  • public void setSlot(int index, Object value)—Sets an indexed member of a JavaScript object. Equivalent to “this[index] = value” in JavaScript.
  • public void removeMember(String name)—Removes a named member of a JavaScript object.
  • public Object call(String methodName, args[])—Calls a JavaScript method. Equivalent to “this.methodName(args[0], args[1], …)” in JavaScript.
  • public Object eval(String s)—Evaluates a JavaScript expression. The expression is a string of JavaScript source code which will be evaluated in the context given by “this”.
  • public String toString()—Converts a JSObject to a String. This method overrides toString() in class Object.
  • public static JSObject getWindow(Applet applet)—Gets a JSObject for the window containing the given applet.
  • protected void finalize()—Finalizing decrements the reference count on the corresponding JavaScript object. This method overrides finalize() in class Object.

JavaScript Exception

The 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 Communication

LiveConnect provides three ways for JavaScript to communicate with Java:

  1. JavaScript can call Java methods directly.
  2. JavaScript can control Java applets.
  3. JavaScript can control Java plug-ins.

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 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. 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 Applets

The 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.

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us