Documentation versions (currently viewingVaadin 14)

You are viewing documentation for an older Vaadin version. View latest documentation

Importing JavaScript, JavaScript modules, and HTML

You can add classic JavaScript source files, JavaScript modules, and HTML imports (compatibility mode) to your host page directly from your Java classes.

Polymer 3 templates should be imported using @JsModule (see Creating Polymer Templates for more information).

There are two ways to add JavaScript and HTML files. Both have the same effect and you can use whichever suits you best.

  1. Using the @JavaScript, @JsModule, and @HtmlImport annotations.

    Example: Importing HTML and JavaScript files into CustomComponent.

    static class CustomComponent extends Component
            implements HasText {
      // implementation omitted
    • All the resource annotations are repeatable. Add one annotation for each file you need to add.

  2. Using the addJavaScript(String url) and addHtmlImport(String url) methods from the Page class. The Page class also has method addJsModule(String url) but it is meant to be used to add an external JavaScript module.

    Example: Using the addJavaScript(String url) and addHtmlImport(String url) methods to import HTML and JavaScript files.

      private void addDependencies() {
        // external JavaScript module

See Storing and Loading Resources for more on storing your resources, configuring advanced resource loading, and resource load ordering.