Annotation Interface JavaScript
Component class. For adding
multiple JavaScript files for a single component, you can use this annotation
multiple times.
It is guaranteed that dependencies will be loaded only once. The files loaded
will be in the same order as the annotations were on the class. However,
loading order is only guaranteed on a class level; Annotations from different
classes may appear in different order, grouped by the annotated class. Also,
files identified by @JavaScript will be loaded after
JsModule and before
CssImport.
NOTE: Bundled frontend resources are combined into one big bundle. This
means, that a bundled JavaScript file loaded by one class will be present on
a view constructed by another class. For example, if there are two classes
RootRoute annotated with @Route(""), and another class
RouteA annotated with @Route("route-a") and
@JavaScript("./src/javascript.js"), the javascript.js will be
run on the root route as well. This does not apply to resources loaded at
runtime, such as a @JavaScript with type() set to
JavaScript.Type.MODULE, which are loaded per page and never bundled.
Values with a runtime prefix (http://, https://, //,
context://, base:// or a leading /) are never bundled
and are loaded at runtime instead, in the same way as
Page.addJavaScript(String). The same applies to a URL with any other
scheme (a value containing ://). Whether the resulting script
element is a classic script or a module is decided by type(). On an
AppShellConfigurator, such values are
added to the initial page as script elements; LoadMode.INLINE
is not supported there. Values on an interface are loaded at runtime for the
components that implement it. On any other class that is not a component,
such as a VaadinServiceInitListener, such values are not loaded at
all. Files loaded at runtime are served by the servlet container from the
same locations as StyleSheet files.
Other paths used in the value() method are considered as
relative to frontend directory and they are bundled and added to the
page as a JavaScript module (a script element with
type="module"). In this case a JavaScript annotation behaves
exactly as a JsModule annotation. This bundled interpretation is
deprecated: use JsModule with the same value to keep bundling the
file, or serve the file as a static resource and reference it with a runtime
prefix (or JavaScript.Type.MODULE) to load it at runtime.
Setting type() to JavaScript.Type.MODULE opts out of that bundling: the
file is then always loaded at runtime as a script element with
type="module", even when the value is a bare relative path, which is
resolved against the servlet context root. Use it for hand-authored or
CDN-hosted ES modules that should not go through Vite; for build-time bundled
ES modules use JsModule instead.
It's not possible to execute a function defined in JavaScript module via
UI.getCurrent().getPage().executeJs("some_function_definied_in_module()");
because the function is private there (unless it's explicitly exposed). The
JavaScript where the function is defined should be loaded at runtime with the
default JavaScript.Type.SCRIPT (for example context://foo.js or an
external URL) or added using Page.addJavaScript(String): in this case
all declared functions become available in the global scope.- Since:
- 1.0
- Author:
- Vaadin Ltd
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic @interfaceInternal annotation to enable use of multipleJavaScriptannotations.static enumThe kind of<script>tag to render for the dependency. -
Required Element Summary
Required Elements -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionbooleanDefines if the JavaScript should be loaded only when running in development mode (for development tooling etc.) or if it should always be loaded.Determines the dependency load mode.The kind of<script>tag to use when loading the file.
-
Element Details
-
value
String valueJavaScript file URL to load before using the annotatedComponentin the browser.URL resolution rules, in order:
http://...,https://...,//...? used unchanged.context://foo.js? resolved against the servlet context root (independent of the Vaadin servlet mapping).base://foo.js? resolved against the page's<base>URI, i.e. the Vaadin servlet mapping path./foo.js? used unchanged as an absolute server path. A value with a leading/that contains a path traversal (..) is rejected with a warning and not loaded.- A URL with any other scheme (a value containing
://) ? used unchanged. - Any other value (bare relative,
"./foo.js","../foo.js") is currently treated as a bundle source for backwards compatibility ? see the class Javadoc on bundled values. Migrate to@JavaScript("context://foo.js")for runtime loading or toJsModulefor bundling.
When
type()isJavaScript.Type.MODULE, the value is loaded at runtime regardless of whether it has a URL prefix; bare relative paths are normalized tocontext://<value>and served as static resources by the servlet container. A value that is not an external URL (no scheme and no leading//) and contains a path traversal (..), such as../foo.js, is rejected with a warning and not loaded.- Returns:
- a JavaScript file URL
-
type
JavaScript.Type typeThe kind of<script>tag to use when loading the file. Defaults toJavaScript.Type.SCRIPT(a classic<script>element). Set toJavaScript.Type.MODULEto render a<script type="module">element instead, e.g. for hand-authored or CDN-hosted ES modules that should not go through Vite. For build-time bundled ES modules useJsModuleinstead.- Returns:
- the kind of script tag to render
- Since:
- 25.4
- Default:
SCRIPT
-
developmentOnly
boolean developmentOnlyDefines if the JavaScript should be loaded only when running in development mode (for development tooling etc.) or if it should always be loaded.By default, scripts are always loaded.
- Returns:
trueto load the script only in development mode,falseto always load it- Since:
- 24.2
- Default:
false
-
loadMode
LoadMode loadModeDetermines the dependency load mode. Refer toLoadModefor the details.LoadMode.INLINEcannot be combined withJavaScript.Type.MODULE; such a combination is rejected with anIllegalArgumentExceptionwhen the dependency is added to the page.- Returns:
- load mode for the dependency
- Default:
EAGER
-