3

I am trying to integrate some js project into my vaadin app. And I tried the methods to invoke external javascript code, only successed on some easy/javascript. I tried the NativeJs lib and the code is:

NativeJS nativeJScomponent = new NativeJS();
nativeJScomponent.setJavascript("alert('foo');");
nativeJScomponent.execute();
addComponent(nativeJScomponent);

However, when I used some other code like:

String jsCode = "<div /><script type=\"text/javascript\">var d = new Date();" 
                  + "var time = d.getHours();" 
                  + "if (time < 10) {document.write(\"<b>Good morning</b>\");}</script>";
NativeJS nativeJScomponent = new NativeJS();
nativeJScomponent.setJavascript(jsCode);
nativeJScomponent.execute();
addComponent(nativeJScomponent);

It failed to display content.

And I also used the customelayout and label to run javascript. The same results came.

Is there any method to integrate vaadin with js?

2
  • 1
    I would think that you're mixing up html with js, while NativeJS probably only supports compiling js. Commented May 19, 2011 at 8:25
  • yes, just like a field with html and js. And I have solved the problem and I use the coustomlayout to do this and need to write the script tag into the applicationservlet class. Commented May 23, 2011 at 19:43

3 Answers 3

3
Window window = new Window("");
window.executeJavascript("javascript:testFunctionCall();");
Sign up to request clarification or add additional context in comments.

Comments

1

The correct way to do this is to implement AbstractJavaScriptComponent. This allows you to include a plain Javascript file in your project and control that library from the server side through RPC calls and shared state.

An easy tutorial can be found here and the Book of Vaadin goes more in-depth here. You can also take a look at the source code of my Vaadin addon which integrates a Javascript media library with Vaadin.

Comments

0

have you looked and tried to run the source in http://uilder.virtuallypreinstalled.com/run/Calling_External_JavaScript/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.