Applet interact with JavaScript

xiaoxiao2021-03-06  61

If you need to use JavaScript objects in Java, you must be in your Java file

Enter the Netscape.javascript package, which is defined as follows:

Netscape.javascript.jsbobject allows Java code to access JavaScript methods and properties;

Netscape.javascript.jsexception allows Java code to process JavaScript exception;

Netscape.plugin.plugin allows client JavaScript and Applets to operate a plugin (

PLUG-IN).

Starting with JavaScript 1.2, these classes are submitted to a file that suffix .jar, and this

The previous JavaScript version, these classes are submitted to a buffet for .zip. For visiting

LiveConnect class, you can have two ways to place .jar or .zip files in JDK compiler

Classpath:

Create a ClassPath environment variable to specify the path and name of these files;

Specify the location of these files when you use the -classpath command line parameters.

Access JavaScript with JSObject

Suppose you have a Java class called Javadog, the Javadog constructor is set.

JavaScript object JSDog, it is defined as a JSObject type, and its code is as follows:

Import netscape.javaScript. *;

Public class javadog {

Public String Dogbreed;

Public String Dogcolor;

Public String Dogsex;

// define the class constructor

Public Javadog (JSObject JSDOG) {

// use try ... catch to handle jsexceptions here

THISDOGBREED = (String) JSDog.getMember ("BREED");

THISDOGCOLOR = (String) JSDog.getMember ("color");

THISDOG.GSEX = (String) JSDog.getMember ("SEX");

}

}

Now let's take a look at how to use Java to access the client JavaScript, but the HTML page must be allowed

A Java Applet Access JavaScript- is specified in the tag

MayScript property. You must get it before you visit JavaScript in Navigator

The handle of the Navigator window, you can use in the Netscape.javaScript.jsObject class

GetWindow method to achieve this. For example, if a variable of a JSOBJECT class is defined

Win, the following code gives the window handle to Win:

Public class myapplet extends applet {

Public void init () {

JSObject Win = JSObject.getWindow (this);

}

}

Handling the exception of JavaScript in JavaCrip

When the JavaScript code called from Java has a runtime error, it will send one

Abnormal, then you can use the try ... catch statement to capture this exception. E.g:

Try {

Global.eval ("foo.bar = 999;");} catch (exception e) {

IF (e instanceof jsexception) {

Jscodefailed () ";

} Else {

OtherCodefailed ();

}

}

Access to JavaScript objects and properties

We use the GetMember method to access the objects and properties of JavaScript, such as the following code.

Allows you to access the JavaScript object Document.testform by variable myform:

Public void init () {

Win = jsobject.getwindow (this);

myform = win.eval ("Document.Testform")

}

You can also use the following statements to replace myform = win.eval ("Document.TestForm"):

JSObject Doc = (jsobject) Win.getMember ("Document");

JSObject myform = (jsobject) Doc.getMember ("testform");

If JavaScript object document.testform.jazz is a Checkbox, the following statement

Allows you to access its checked properties:

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");

}

Access JavaScript method

Method in the Netscape.javascript.jsobject class allows you to ask for any

JavaScript expressions, first with getWindow to get the handle of the JavaScript window, then use

EVAL to access a JavaScript method as follows:

JSObject.getWindow (). Eval ("expression")

转载请注明原文地址:https://www.9cbs.com/read-119532.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.039, SQL: 9