如何使用JSNI从GWT Java运行JavaScript函数? [英] How to run JavaScript function from GWT Java with JSNI?

查看:84
本文介绍了如何使用JSNI从GWT Java运行JavaScript函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法从手册中了解:如何从Java运行JS函数?

例如,我在我的html页面中有一个函数:

 < script type =text / javascriptlanguage =javascript> 
函数foo(){
alert('Foo!');
}
< / script>

以下模块显示两个按钮,其中只有第二个按钮可用:

  public class Test_GoogleWeb_JSNI_01 implements EntryPoint {
$ b $ public void onModuleLoad(){

Button fooButton = new Button( 富!);
fooButton.addClickHandler(new ClickHandler(){
public void onClick(ClickEvent event){
fooRunner();
};
});


HTML fooButtonNative = new HTML();
fooButtonNative.setHTML(< input type ='button'value ='Foo Native'onclick ='foo()'>);

RootPanel rootPanel = RootPanel.get();
rootPanel.add(fooButton);
rootPanel.add(fooButtonNative);


$ b public static native void fooRunner()/ * - {
foo();
} - * /;





$ b

在手册中说,嵌套框架内实现的原生函数,解释情况。但是如何运行JS函数呢?



更新1
以下工作。



Java:

  public static native void fooRunner()/ *  -  {
$ doc.fooRunner ();
} - * /;

JS:

 < script type =text / javascriptlanguage =javascript> 
document.fooRunner = function foo(){
alert('Foo!');
}
< / script>

有没有更好的方法?

解决方案

你自己回答了你的问题。有一个非常简单的理由没有更好的方法:有多种方式来部署GWT应用程序,在iframe中运行只是其中一个选项。所以这就是为什么你必须使用$ wnd变量来访问外部JS函数,所以如果你切换链接器,你的代码仍然可以正常工作。


Can't understand from the manual: how actually to run JS function from Java?

For example, I have a function in my html page:

<script type="text/javascript" language="javascript">
    function foo() {
        alert('Foo!');
    }
</script>

The following module shows two buttons, only second of which works:

public class Test_GoogleWeb_JSNI_01 implements EntryPoint {

public void onModuleLoad() {

    Button fooButton = new Button("Foo!");
    fooButton.addClickHandler(new ClickHandler(){
        public void onClick(ClickEvent event) {
            fooRunner();
        };
    });


    HTML fooButtonNative = new HTML();
    fooButtonNative.setHTML("<input type='button' value='Foo Native' onclick='foo()'>");

    RootPanel rootPanel = RootPanel.get();
    rootPanel.add(fooButton);
    rootPanel.add(fooButtonNative);

}

public static native void fooRunner() /*-{
  foo();
}-*/;
}

It is said in manual, that native functions implemented within nested frame, which explains the situation. But how to run JS functions then?

UPDATE 1 The following works.

Java:

public static native void fooRunner() /*-{
  $doc.fooRunner();
}-*/;

JS:

<script type="text/javascript" language="javascript">
    document.fooRunner = function foo() {
        alert('Foo!');
    }
</script>

Is there a better way?

解决方案

You answered your question yourself. There is no better way for a very simple reason: there are multiple ways to deploy GWT app, running in iframe is only one of the options. So that's why you have to use $wnd variable to access external JS function, so in case if you switch the linker , your still code will work just fine.

这篇关于如何使用JSNI从GWT Java运行JavaScript函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆