Kyleinincubator
Kyleinincubator

Reputation: 313

vaadin javascript

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?

Upvotes: 3

Views: 4365

Answers (3)

bdunn
bdunn

Reputation: 482

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.

Upvotes: 1

fmucar
fmucar

Reputation: 14558

Window window = new Window("");
window.executeJavascript("javascript:testFunctionCall();");

Upvotes: 3

CRH
CRH

Reputation: 321

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

Upvotes: 0

Related Questions