0

Im trying to call a javascript function out of my Vaadin Portlet.

lets say I have an HTML file witch is located in my project ;

homepage.html

<html>  
   ...  
   <script type="text/javascript">  
   ...  
   function foo(String msg)   
   {  
      alert(msg);  
   }  
   ...  
  </script>  
   ...  
</html>  

the page in Embedded in my Portlet via the Vaadin Embedded Browser

how do I call the function foo(String msg) out of my java application

do i need to import/read the homepage.html file and just call it or is it something else I have to do ?

Kiesa
  • 407
  • 1
  • 19
  • 42

3 Answers3

1

firstly you need to get the script body; then you can user javax.script.ScriptEngineManager to solve your problem javax.script.*
pseudo code

import javax.script.*;
ScriptEngine engine = 
            new ScriptEngineManager().getEngineByName("javascript");
String script = getScript(path_to_html);
            engine.eval(script);
Frei Zhang
  • 417
  • 6
  • 12
  • And then there is the problem of what to do when the script tries to interact with what it presumably thinks is the web browser that hosts the HTML it is embedded it. Notice that the sample script says `alert()`. What is that supposed to do in this context? – Thilo Jul 20 '12 at 07:07
  • if your application is applet ,you can parse the script and eval in current context,if your application is desktop application you may need to embed a webbrowser and if your application is web project,just include the page or get script then write to your page – Frei Zhang Jul 20 '12 at 07:12
  • Apparently it is a Vaadin portlet using their WebEmbed component: http://demo.vaadin.com/sampler#WebEmbed (which I guess is an iframe). – Thilo Jul 20 '12 at 07:18
1

The simplest way to include an external javascript file into a Vaadin application is to override the Application#writeAjaxPageHtmlVaadinScripts method.

To call a javascript function from the Vaadin server-side code, you call Window#executeJavascript

@Override
protected void writeAjaxPageHtmlVaadinScripts(Window window,
                                              String themeName, Application application, BufferedWriter page,
                                              String appUrl, String themeUri, String appId,
                                              HttpServletRequest request) throws ServletException, IOException {
  page.write("<script type=\"text/javascript\">\n");
  page.write("//<![CDATA[\n");
  page.write("document.write(\"<script language='javascript' src='" + appUrl + "/VAADIN/scripts/example.js'><\\/script>\");\n");
  page.write("//]]>\n</script>\n");
  super.writeAjaxPageHtmlVaadinScripts(window, themeName, application,
      page, appUrl, themeUri, appId, request);
}

NB : I have never used Vaadin as a Portlet, but a quick look suggests that this should work OK.

However, this approach is rather rudimentary, and only suitable for a quick hack/proof-of-concept: if you want to so anything more sophisticated, then developing your own Vaadin widget is correct approach. It gives you the power of GWT and JSNI, and gives you a much finer grain of control : See The Book Of Vaadin for more details.

Charles Anthony
  • 3,155
  • 17
  • 21
0

Refer to following links, these provides API for doing what you want to do,

http://www.ibm.com/developerworks/java/library/j-5things9/index.html

http://metoojava.wordpress.com/2010/06/20/execute-javascript-from-java/

Rahul Borkar
  • 2,742
  • 3
  • 24
  • 38