sbolla
sbolla

Reputation: 711

Close the browser window in Vaadin

I have google'd quite a lot but was not able to find a solution for this problem. I have a Vaadin application that runs in a browser window. I have a logout button on it, clicking on it must invalidate the session and close the browser window. I was able to get this code to invalidate the session and close the application. However, I am looking to close the browser window also, which is where I am not having any success

WebApplicationContext webCtx = (WebApplicationContext) appRef.getMainWindow().getApplication().getContext();
                HttpSession session = webCtx.getHttpSession();
                session.invalidate();
                appRef.getMainWindow().getApplication().close();

I am using vaadin 6.x and tried the following but they don't work on browsers I tried which is Chrome and IE.

            appRef.getMainWindow().executeJavaScript("window.close();");

Any ideas would be greatly appreciated. Another question I have is do I need to get the name of the main Window and then call mainWindow.close(); or just window.close() ?

Upvotes: 1

Views: 4621

Answers (1)

Jan Galinski
Jan Galinski

Reputation: 11993

  1. Read https://vaadin.com/book/vaadin6/-/page/application.close.html if you haven't done so far.
  2. If appRef is already your Application, you do not have to call appRef.getMainWindow().getApplication() to get the Application. Just do appRef.close();
  3. Do not invalidate the session manually. It messes with the vaadin lifecycle, so the next lines are not executed anymore, at least not in the vaadin context. Just do "application.close()" and let vaadin do the rest.
  4. In vaadin 6 "window.close()" works, i use it with IE and chrome. So after you removed the session invalidation stuff, your code appRef.getMainWindow().executeJavascript("window.close()"); will work as expected.

Upvotes: 3

Related Questions