applet与script交互应用

import java.applet.Applet;

//jre/lib/plugin.jar
import netscape.javascript.JSObject;

public class ScriptApplet extends Applet {

public static JSObject getJSWin(Applet applet) {
JSObject win = JSObject.getWindow(applet);
return win;
}

public static JSObject getDocObject(Applet applet) {
JSObject win = getJSWin(applet);
JSObject doc = (JSObject) win.getMember("document");
return doc;
}

public static JSObject getFormObject(Applet applet, String formName) {
JSObject doc = (JSObject) getDocObject(applet);
JSObject form = (JSObject) doc.getMember(formName);
return form;
}

public static void getFormElementValue(Applet applet) {
JSObject form = (JSObject) getFormObject(applet, "form");
JSObject textField = (JSObject) form.getMember("textField");
String textValue = (String) textField.getMember("value"); // 获取文本区的值

JSObject win = getJSWin(applet);
win.eval("alert(\"This alert comes from Java!\")"); // 调用JavaScript的alert()方法
}

public static JSObject getAllObject(Applet applet) {
JSObject doc = (JSObject) getDocObject(applet);
JSObject all = (JSObject) doc.getMember("all");
return all;
}

public static Object callJSMethod(Applet applet, String methodName,
Object[] params) {
JSObject win = getJSWin(applet);
return win.call(methodName, params);
}
}

你可能感兴趣的:(JavaScript,java)