2012-04-07 56 views
1

我有helloJava.HTML在我的GWT項目,該HTML文件中,我有這個JavaScript代碼如何從Java訪問某些JavaScript文件中GWT

     <script type="text/javascript"> 
function testJavaScript(var input){ 
    var var1inJS = "Default value"; 

    alert("Value of Var1 = " + var1inJS); 
    var1inJS = input; 
    alert("Value of Var1 = " + var1inJS); 

    var var2inJS = "Waht is the value of Var2"; 

    alert("Value of Var2 = " + var2inJS); 

} 

現在我想從調用這個方法我onmoduleLoad類(即從我的java類)。

是否有可能?

hellojava.html文件

      <!doctype html> 
<!-- The DOCTYPE declaration above will set the  --> 
<!-- browser's rendering engine into    --> 
<!-- "Standards Mode". Replacing this declaration --> 
<!-- with a "Quirks Mode" doctype is not supported. --> 

<html> 
    <head> 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 

    <!--                --> 
    <!-- Consider inlining CSS to reduce the number of requested files --> 
    <!--                --> 
    <link type="text/css" rel="stylesheet" href="HelloJSNI.css"> 

    <!--           --> 
    <!-- Any title is fine       --> 
    <!--           --> 
    <title>Web Application Starter Project</title> 

    <!--           --> 
    <!-- This script loads your compiled module. --> 
    <!-- If you add any GWT meta tags, they must --> 
    <!-- be added before this line.    --> 
    <!--           --> 
    <script type="text/javascript" language="javascript" src="hellojsni/hellojsni.nocache.js"></script> 

    <script type="text/javascript"> 
    function testJavascript(var input){ 
     window.jsniAlert(); 
     var var1inJS = "Default value"; 

     alert("Value of Var1 = " + var1inJS); 
     var1inJS = input; 
     alert("Value of Var1 = " + var1inJS); 

     var var2inJS = "Waht is the value of Var2"; 

     alert("Value of Var2 = " + var2inJS); 

    } 

    function callJava(){ 

    } 
    </script> 
    </head> 

    <!--           --> 
    <!-- The body can have arbitrary html, or  --> 
    <!-- you can leave the body empty if you want --> 
    <!-- to create a completely dynamic UI.  --> 
    <!--           --> 
    <body> 

    <!-- OPTIONAL: include this if you want history support --> 
    <iframe src="javascript:''" id="__gwt_historyFrame" tabIndex='-1' style="position:absolute;width:0;height:0;border:0"></iframe> 

    <!-- RECOMMENDED if your web app will not function without JavaScript enabled --> 
    <noscript> 
     <div style="width: 22em; position: absolute; left: 50%; margin-left: -11em; color: red; background-color: white; border: 1px solid red; padding: 4px; font-family: sans-serif"> 
     Your web browser must have JavaScript enabled 
     in order for this application to display correctly. 
     </div> 
    </noscript> 

    <h1>JSNI EXAMPLE</h1> 

    <table align="center"> 
     <tr> 
     <td colspan="2" style="font-weight:bold;"></td>   
     </tr> 
     <tr> 
     <td id="nameFieldContainer"></td> 
     <td id="sendButtonContainer"></td> 
     </tr> 
     <tr> 
     <td colspan="2" style="color:red;" id="errorLabelContainer"></td> 
     </tr> 
    </table> 
    </body> 
</html> 

回答

0

特拉維斯該函數的語法是正確的,問題是調用函數的名稱,JS腳本不是,而是

下面是更正

  1. 錯誤在函數聲明,你不應該爲 參數使用var

  2. 刪除第一行window.jsniAlert();

  3. 校正第二函數function callJava(){的聲明function callJava(){

下面是代碼(允許調用JS testJSnative的功能):

<script type="text/javascript"> 
function testJSnative(input){ 
    //window.jsniAlert(); 
    var var1inJS = "Default value"; 

    alert("Value of Var1 = " + var1inJS); 
    var1inJS = input; 
    alert("Value of Var1 = " + var1inJS); 

    var var2inJS = "What is the value of Var2"; 

    alert("Value of Var2 = " + var2inJS); 

} 

function callJava(){ 

} 
</script> 
的類實施 EntryPoint

實施例:

public void onModuleLoad() { 

    Button jsniButton = new Button("call JS"); 
    jsniButton.addClickHandler(new ClickHandler() { 

     @Override 
     public void onClick(ClickEvent event) { 

     callJavascript(); 
     } 
    }); 
    RootPanel.get().add(jsniButton); 
    } 

    public native void callJavascript() /*-{ 
    $wnd.testJSnative("hello"); 
    }-*/; 

我希望這會有所幫助。

+0

謝謝你,我的另一種方法callJava(),我想調用此方法的Java類,任何sugestion這個 – user1226162 2012-04-09 15:09:49

+0

@ user1226162不客氣。在此處查找有關GWT JSNI的更多信息:https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI – 2012-04-10 07:10:29

2

是。這大約是你想要什麼:

public static native void callJavascript() /*-{ 
    $wnd.testJavaScript("hello"); 
}-*/; 

,然後調用此方法在Java代碼:callJavascript();

閱讀JSNI谷歌的文檔: https://developers.google.com/web-toolkit/doc/latest/DevGuideCodingBasicsJSNI

+0

嘗試用自己的方式,它給我這樣的例外:javascriptexception:對象犯規支持屬性或方法「testjavascript」 – user1226162 2012-04-07 03:10:07

+0

你正確把握'testJavascript'? – 2012-04-07 03:18:42

+0

是的,我做到了,因爲它是在JavaScript中,在相同的情況下 – user1226162 2012-04-07 03:21:38

相關問題