小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Call Javascript from a Java applet - Real‘s Java How-to

 ShangShujie 2007-06-10
Jump to real‘s how-to main page

Call Javascript from a Java applet

Netscape only, using the javascript: protocol
A Javascript function is called from Java by using the showDocument method. A URL is needed with "javascript:" as the protocol.

[Java applet]

import java.applet.*;
import java.net.*;
public class InJava4 extends Applet{
public void init(){
String msg = "Hello from Java (using javascript alert)";
try {
getAppletContext().showDocument
(new URL("javascript:doAlert(\"" + msg +"\")"));
}
catch (MalformedURLException me) { }
}
}

[Javascript and HTML]

<HTML><HEAD></HEAD><BODY>
<SCRIPT>
function doAlert(s) {
alert(s);
}
</SCRIPT>
<APPLET CODE="InJava4.class"
NAME="myApplet"  MAYSCRIPT
HEIGHT=10 WIDTH=10>
</APPLET>
</BODY>
</HTML>
Try it here

Netscape and IE ok, using the netscape.javascript.JSObject package

How to compile when using the netscape.javascript.JSObject package ?

For Java 1.4.2 and later: add plugin.jar to your classpath. It can be found in the lib directory of your JRE installation, e.g. C:\Program Files\Java\jre1.5.0\lib\plugin.jar

For Java 1.4.0/1.4.1: use jaws.jar (same directory).

In the following example, you type in the TextField a Javascript function and press the button to execute the function. For example, try alert(‘Hello from JAVA‘). Or you can execute function defined on the same page as the Applet. The Applet must contains the MAYSCRIPT parameter to be able to use JSObject.

Netscape and IE Ok.

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;
public class InJava5 extends Applet implements ActionListener {
Button b;
TextField t;
public void init() {
t = new TextField(20);
add(t);
b = new Button("execute Javascript");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
JSObject win = (JSObject) JSObject.getWindow(this);
win.eval(t.getText());
}
}
}
Try it here

Another way is to use the Reflection API. That way you don‘t need to modify your CLASSPATH for compilation or even import the netscape.jsobject package.

// posted by C Werner on the realhowto list
import java.lang.reflect.*;
...
// Somewhere in the applet class ...
...
String jscmd = "window.close()";  /* JavaScript command */
String jsresult = null;
boolean success = false;
try {
Method getw = null, eval = null;
Object jswin = null;
Class c =
Class.forName("netscape.javascript.JSObject"); /* does it in IE too */
Method ms[] = c.getMethods();
for (int i = 0; i < ms.length; i++) {
if (ms[i].getName().compareTo("getWindow") == 0)
getw = ms[i];
else if (ms[i].getName().compareTo("eval") == 0)
eval = ms[i];
}
}
Object a[] = new Object[1];
a[0] = this;               /* this is the applet */
jswin = getw.invoke(c, a); /* this yields the JSObject */
a[0] = jscmd;
Object result = eval.invoke(jswin, a);
if (result instanceof String)
jsresult = (String) result;
else
jsresult = result.toString();
success = true;
}
catch (InvocationTargetException ite) {
jsresult = "" + ite.getTargetException();
}
catch (Exception e) {
jsresult = "" + e;
}
if (success)
System.out.println("eval succeeded, result is " + jsresult);
else
System.out.println("eval failed with error " + jsresult);

    本站是提供個人知識管理的網(wǎng)絡存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多