不惜一切代价!!!可以加到143分!
本文导语: html页面中可以用, 也可以用, 请教诸位大侠: 已经有了一个applet的class, 能不能用将它嵌入页面? 如果可能,classID如何得到? 最好能给个例子。 我的最终目的是用 applet 控制 dhtml, 动态嵌入 applet. com.ms.wfc.html...
也可以用,
请教诸位大侠:
已经有了一个applet的class,
能不能用将它嵌入页面?
如果可能,classID如何得到?
最好能给个例子。
我的最终目的是用 applet 控制 dhtml,
动态嵌入 applet.
com.ms.wfc.html可以使用 java 控制 dhtml,
但是只有 DhClassFactory 类。
谢谢谢谢。
|
Netscape only
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 inJava extends Applet{
public void init(){
String msg = "We will jump to an HTML tag";
try {
getAppletContext().showDocument
(new URL("javascript:doAlert("" + msg +"")"));
getAppletContext().showDocument
(new URL("javascript:jumpTo("#JUMP")"));
}
catch (MalformedURLException me) { }
}
}
[Javascript and HTML]
function doAlert(s) {
alert(s);
}
function jumpTo(tag) {
self.location=tag;
}
jump here from Java via Javascript
Netscape AND Microsoft Internet Explorer v4
With IE4, Microsoft included the Netscape package netscape.javascript.* to allow a JAVA applet access to Javascript objects. To compile such Applet, you must adjust the CLASSPATH to include the Netscape java40.jar. To use the Netscape JAR, the CLASSPATH will look like this: set CLASSPATH=
.;c:windevjdk1.1.3libclasses.zip;
c:Program FilesNetscapeCommunicatorProgramJavaClassesjava40.jar;
Note that your CLASSPATH may be different depending on your JDK installation/version
To compile with IE, first locate the javapackages directory in the Windows or WinNt directory. You will find a bunch a zip files with strange names. On my installation (IE5), the file Qk1r3vrp.zip is the one containing the 2 classes you need, JSObject.class and JSException.Class. Include this zip in the classpath before compiling (or you can extract the 2 files (with directory) and create a new smaller ZIP (again with the directory) and use the new zip in the classpath). Note that if you compile with the Microsoft compiler, you don't need to do this.
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. import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;
public class jsJAVA 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());
}
}
}
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
您可能感兴趣的文章:
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。