Execute JavaScript at beginning of Application


What is the best way to execute JavaScript before the application starts? In my case I have to do this:

But I need to do this only for one of my applications


You can extend ApplicationServlet and override writeAjaxPageHtmlVaadinScripts() much as mentioned
here regarding writeAjaxPageHtmlHeader()

There’s also a summary of overriding writeAjaxPageHtmlHeader() with some example code in Knowledge Base article
#307: How do I load extra JavaScript libraries?
(Pro Account required).

Is there a clever way to do this only for one of my applications? The only solution which comes to mind is to check for the request’s URL


The servlet class is defined in the web.xml descriptor and it’s per-Vaadin-application, as described

What I meant was that I have two classes extending BaseApplication.
One should call the JavaScript, the other should not.


I still do not quite understand what you want to do. Is the BaseApplication your own class?

Once the application has started, you can use Window.executeJavaScript() to run certain JavaScript commands (though not actually inject JavaScript).

The JavaScript code you mentioned does a redirect. Is that what you want? Once a Vaadin application has started, you can do redirect to another URL with Window.open(). Using the executeJavaScript() for that purpose is also possible. Also setting setLogoutURL() and doing close() for the application is possible and cleans up the session as well.


I don’t have to inject Javascript at all, so I will just call Window.executeJavaScript()