Where to put Javascript methods?

Hello Vaadin Community,

I want to be able to add my own javascript methods in my code.
So far I know that they way to do this is to declare javascript functions inside native methods, such as this

	public native static void initializeMethods() /*-{
 		$wnd.printHello = function() {

What is confusing me is where in the code to call these native methods, in my case where in the code to make the call to the native method initializeMethods(). Making the call to this method from a class that implements a vaadin server side component throws an error (“java.lang.UnsatisfiedLinkError:”).

Looking at the GWT examples, I notice that they make the call before loading the components, which made me believe i should be making the call to the method in the client components. However, my code consists of classes implementing server-side components only since I only use the Default widgets and have no need to create my own widgets or create my own client components.

So my question is, if I am only creating classes that implement vaadin server side components, where in the code should I make the call to the native methods declaring the javascript functions?

Thanks for your time.


Calling javascript is for the client-side (GWT) only, not on the server (unless you use a javascript engine on the server-side, but I suspect that’s not what you’re after). Also, you can only call client-side methods from javascript, so if you want to call
, you’ll have to (e.g) implement a component which has a client-side widget that exposes a printHello() method and communicates with the server-side counterpart which in turn calls org.vaadin.ui.MyApplication.printHello().

I.e. it seems you’re trying to do something that is not supported - though I might of course have misunderstood what you’re trying to achieve…

Hope that clears things up a little.

Best Regards,

Thanks for the clarification Marc. Now I know I have to find another way to implement this, most likely figure out a way for client side methods to communicate with server side methods.


Could you please tell me how did you solve this problem? I am having issues in adding some javascript to the client side from vaadin application?

My portlet client side should have the below javascript embedded, any help is appreciated.


Old thread, but how about overriding e.g. AbstractApplicationPortlet.writeAjaxPageHtmlVaadinScripts() and - in addition to calling the superclass method - writing your own scripts.