当前位置: 技术问答>java相关
请问高手,java applet调用javascript函数的一个问题
来源: 互联网 发布时间:2015-08-10
本文导语: java applet调用javascript函数时,如何向javascript函数传参数,并取得返回值? 十分紧急,请高手帮忙,谢谢! | 是的!传递参数的详细实现如下: import java.awt.*; import java.awt.event.*; import java.applet...
java applet调用javascript函数时,如何向javascript函数传参数,并取得返回值?
十分紧急,请高手帮忙,谢谢!
十分紧急,请高手帮忙,谢谢!
|
是的!传递参数的详细实现如下:
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import netscape.javascript.*;
public class MyClass extends java.applet.Applet implements ActionListener
{
Button bt = new Button("Exec");
String str = Math.random()+"";
public void init()
{
bt.addActionListener(this);
this.add(bt);
}
public void paint(Graphics g)
{
g.drawString(str,10,10);
}
public void actionPerformed(ActionEvent ev) {
String[] param= {""+Math.random()};
try{
str = (String)JSObject.getWindow(this).call("doTest",param);
}catch(Exception exp)
{
this.showStatus(exp.toString());
str = param[0];
}
repaint();
}
}
测试的html文件如下:
New Document For Using Applet
Hello This is a Test!
function doTest(vdt)
{
测试的html文件如下:
New Document For Using Applet
Hello This is a Test!
function doTest()
{
alert(vdt);
return "Pass in is:"+vdt+"nReturn is: "+vdt;
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import netscape.javascript.*;
public class MyClass extends java.applet.Applet implements ActionListener
{
Button bt = new Button("Exec");
String str = Math.random()+"";
public void init()
{
bt.addActionListener(this);
this.add(bt);
}
public void paint(Graphics g)
{
g.drawString(str,10,10);
}
public void actionPerformed(ActionEvent ev) {
String[] param= {""+Math.random()};
try{
str = (String)JSObject.getWindow(this).call("doTest",param);
}catch(Exception exp)
{
this.showStatus(exp.toString());
str = param[0];
}
repaint();
}
}
测试的html文件如下:
New Document For Using Applet
Hello This is a Test!
function doTest(vdt)
{
测试的html文件如下:
New Document For Using Applet
Hello This is a Test!
function doTest()
{
alert(vdt);
return "Pass in is:"+vdt+"nReturn is: "+vdt;
}
}