如何从SWT与Javascript函数通信 [英] How to talk to a Javascript function from SWT
问题描述
我的HTML文件有一个javascript函数xxx_return(),它将返回一个字符串值。有什么方法可以从Java层获取这个值吗?。
My HTML file has a javascript function xxx_return(), which will return a string value. Is there any way i can take this value from Java layer?.
我使用SWT shell来显示这个html。 SWT是否有任何功能来获取脚本函数的返回值?
I am using SWT shell to display this html. Does SWT carry any feature to get the return values of a script function?
编辑:
我的代码是如下所示:
package test.html.simulation;
My code is something like below: package test.html.simulation;
import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTException;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class BrowserExample{
public static void main(String[] args)
{
Display display = new Display();
final Shell shell = new Shell(display);
String html="";
Object ob=null;
shell.setText("Browser Example");
shell.setSize(500, 350);
final Browser browser = new Browser(shell, SWT.NONE);
browser.setBounds(5, 75, 600, 400);
browser.setUrl("http://localhost/test/tryxml.html");
shell.open();
//System.out.println(browser.getUrl());
//try
{
html=(String)browser.evaluate("returnHTML();");
}/*catch(SWTException e)
{
System.out.println(e.getMessage());
}*/
System.out.println(html);
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
这段代码给我一个类似SWT的例外情况预期的对象:
This code gives me an SWT Exception like Object expected:
Exception in thread "main" org.eclipse.swt.SWTException: Object expected
at org.eclipse.swt.browser.WebBrowser$EvaluateFunction.function(Unknown Source)
at org.eclipse.swt.browser.WebSite.Invoke(Unknown Source)
at org.eclipse.swt.browser.WebSite$7.method6(Unknown Source)
at org.eclipse.swt.internal.ole.win32.COMObject.callback6(Unknown Source)
at org.eclipse.swt.internal.ole.win32.COM.VtblCall(Native Method)
at org.eclipse.swt.internal.ole.win32.IDispatch.Invoke(Unknown Source)
at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source)
at org.eclipse.swt.ole.win32.OleAutomation.invoke(Unknown Source)
at org.eclipse.swt.browser.IE.execute(Unknown Source)
at org.eclipse.swt.browser.WebBrowser.evaluate(Unknown Source)
at org.eclipse.swt.browser.Browser.evaluate(Unknown Source)
at test.html.simulation.BrowserExample.main(BrowserExample.java:29)
在我写的java脚本中脚本标记中的函数如:
In the java script i have written a function in the script tag like:
<script>
function returnHTML()
{
var str=document.body.innerHTML;
//alert(str);
return str;
}
</script>
任何人都可以在此找到错误吗?我不明白它在哪里遇到错误。
Can anyone find the error in this?. I don't understand where it hits the error.
谢谢。
推荐答案
使用SWT 浏览器
对象。然后你可以简单地使用 String result =(String)Browser.evaluate(xxx_return();)
。
Use an SWT Browser
object. Then you can simply use String result = (String)Browser.evaluate("xxx_return();")
.
这篇关于如何从SWT与Javascript函数通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!