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

The HTML definition for the applet in Example 31-3 is as follows:

<APPLET CODE="nervous" WIDTH=460 HEIGHT=60 NAME="nervousText">
<PARAM NAME="text" VALUE="Nervous Text">
<PARAM NAME="speed" VALUE="100">
<PARAM NAME="font" VALUE="Arial">
<PARAM NAME="size" VALUE="36">
<PARAM NAME="style" VALUE="plain">
</APPLET>

The CODE attribute points to the compiled applet, saved with the filename extension .class. Notice that the extension specification can be omitted in this case. Some applets let the HTML author set some initial values by the <PARAM> tags. Each parameter’s name is defined in the Java applet code, while its value is set as the applet initializes itself by the <APPLET> statement. Another feature of LiveConnect is the ability to modify the applet’s parameters while the applet is running. Once the applet is running, these parameters are accessible from JavaScript or HTML but only with LiveConnect.

You can access the applet using one of several procedures. References begin with the container object, document, followed by the applet name (by name or position in the document.applets[i] array), and then either the applet’s variable or method name. Last, there are arguments (enclosed in parentheses) to be passed to the applet. Here are the four possible references:

document.appletName.varName
document.appletName.methodName(arguments)
document.applets[i].varName
document.applets[i].methodName(arguments)

Bear in mind that varName and methodName should be defined as public in their class. Now, take another look at the HTML code in Example 31-3. The applet is named nervousText via the NAME attribute. The Stop and Start buttons are defined as follows (pay special attention to their event handlers):

<INPUT TYPE="button" VALUE="start"
onClick="document.nervousText.start()">
<INPUT TYPE="button" VALUE="stop"
onClick="document.nervousText.stop()">

The first button invokes the applet’s start() method. It is easy to see that this method belongs to the applet, because it is preceded by the applet’s standard reference—document.nervousText. The second button’s onClick event handler calls the nervousText applet’s stop() method. Notice that the name of the applet’s class (which is also the name of the .class file) is irrelevant when invoking an applet’s method or handling a public variable from JavaScript.

What Else is Scriptable in an Applet?

The best way to find out what methods are available for scripting is to investigate the source code on your own. Most applets currently in circulation on the Web were not designed for external access, so there are probably only a few public methods defined that will let scripters control the applet. Since the documentation for most applets does not deal with minor details such as variable declarations and method definitions, you will have to study the source code for the desired Java class.

As you study the code, focus on searching the public keyword. Also, pay attention to the data type that a method returns. The following declaration, for instance, belongs to a method that returns a string via a return statement:

public String methodName(parameters)

A method that does not return a value is defined with the keyword void.

Modifying an Applet

Modifying simple applets to be scriptable is usually not a difficult task. Going through some basic Java tutorials is a good idea to start with. If you are an experienced Java programmer, go ahead and write your own applets. If you are not a native Java developer, however, try playing around with an existing applet’s source code. First, look for variables that have an important role in the applet. In an applet that involves text, for example, look for a variable that holds the name of the current font. If you cannot find such a variable, search for a font name expressed as a literal in the code, and try replacing that literal with your own variable (define it as a String, of course).

Once you’ve spotted variables that you would like to control via JavaScript, write public methods to control the value of these variables. Although it is also possible to access public instances (variables) from JavaScript, we recommend that you modify the applet entirely by methods. Looking back at the source code for nervous.class (Example 31-3a), there are no methods designed for JavaScript access. Nevertheless, there are quite a few instances that we would like to control by JavaScript, such as fontName, fontSize, fontStyle, and text. Here is a modified version of that example, explicitly designed for JavaScript access:

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us