Execute JavaScript at beginning of Application

Hi,

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

Thanks,
Roland

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

Thanks,
Roland

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

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

Thanks,
Roland

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.

Thanks!

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