executeJavaScript and window initialization order problem


Currently, i am developing a webapp using Vaadin (version 6.4.4) and i have encountered a problem when using the executeJavaScript feature.

This is the situation:

I have a window that has a button on it saying “Print this window”, when the user clicks on it, it will open a new window with the content that should be printed and should automaticly open the printer-dialog.

The printable-window initiates all its content in the constructor, and sets data into it from an entity (the window uses form elements such as tables, textfields and datefields). All the contents should be printed into the report.

The problem:

When opening the window, some of the contents (tables) get initiated with the correct values before the printer-dialog opens, but the form object (textfields and datefields) are not.
For some reason the executeJavaScript is being used before the entire window is initialized. This is a problem because the printer-dialog locks everything else so the rest of the page is not initializing properly.

Is there any way to tell vaadin to send the executeJavaScript after a given moment, or after the entire constructor of a window is done?

Thanks in advance.

Could you use setTimeout() in your JavaScript to ensure that everything has been loaded properly or would you consider it to be to too fragile for this purpose?

BTW: In many cases it is better to output simplified HTML to the window to be printed than to use Vaadin components like Table and to try to print them.

Thanks for your reply Joonas,

setTimeout() would not be a good solution for this problem, because you never know when the page is actually finished.

Is there a way to use the vaadin components and generate plain html or pdf (or similar) so it can be printed?
It is not desired to build an entire html report just for printing this page (the lay-out is quite complicated and will change in the future).

Vaadin TestBench product has implemented this in roughly the following manner. You might use this to wait for vaadin to finish rendering before printing, but I have not tested it:

if (wnd.wrappedJSObject) {
		wnd = wnd.wrappedJSObject;

var connector = null;
if (wnd.itmill) {
	connector = wnd.itmill;
} else if (wnd.vaadin) {
	connector = wnd.vaadin;

var clients = connector.clients;
if (clients) {
			  for ( var client in clients) {
				  if (clients[client]
.isActive()) {
					  return false;
			  return true;
		  	} else {
		  		//A Vaadin connector was found so this is most likely a Vaadin application. Keep waiting.
		  		return false;
	 }, timeout);

There is no separate rendering pipeline for static HTML or PDF. So your options are to either to 1) create the static html or pdf for printing with some other tool, 2) use long enough setTimeout or 3) get the above wait for vaadin javascript approach to work.

Thanks for your reply.

I am trying to implement the javascript you’ve provided, but it seems that a part is missing.
The following line is unclear to me because i am missing a part of the code.

}, timeout);

Can you provide me with the complete code to use/test this feature, or point me to the location of the code?

Thanks in advance.

I am sorry, but I have not tested the code at all. I am sure that it needs some work before it can be used.

The timeout is just a integer given in milliseconds. You could replace it with 100. If you get it to work, please keep this thread updated.

  • Joonas

I think i have the logic, but how do i include a .js file into a specific window?
The code is executable through a functioncall, so executeJavascript can trigger the function.

How about just executing the whole thing through executeJavaScript() - would be simpler…