Don’t know if this applies to your use case but the normal ways to add JS to your application is through extending ApplicationServlet and overriding writeAjaxPageHtmlHeader() - or then by calling window.executeJavaScript(script);.
Actually, I have a custom layout with several DIVs. When I click on one of them, I want to extend / collapse its content without calling the server (so with javascript).
The javascript code is quite big so it is not really “clean” if I embed it in the custom layout’s HTML file.
That’s why I was wondering how to call javscript from my custom layout.
Jens already gave the best answer
here ; if you want to have your javascript library imported once into the browser, you must override the writeAjaxPageHtmlHeader method in the ApplicationServlet.
I’ve never tried to import or write javascript in a CustomLayout; off of the top of my head, I can’t see any reason why it wouldn’t work.
com.vaadin.terminal.gwt.client.ui.VCustomLayout has some extra logic for script block handling (see method extractBodyAndScriptsFromTemplate).
The scripts are extracted and evaluated separately (maybe because it doesn’t work with setInnerHTML).
I managed to define a javascript function within a custom layout template using the following approach:
I think it’s not an answer. Custom Layout has very limited functionality and as for me this is a bug. If Custom Layout could parse HTML and add there Vaadin Components it should also could add some CSS that this HTML page want to use!