How in Vaadin 12 call JavaScript's method?

596 views Asked by At

In my Vaadin 12 project:

import com.vaadin.flow.component.dependency.HtmlImport
import com.vaadin.flow.component.dependency.JavaScript
import com.vaadin.flow.component.dependency.StyleSheet
import com.vaadin.flow.component.html.Div
import com.vaadin.flow.component.page.BodySize
import com.vaadin.flow.component.page.Page
import com.vaadin.flow.router.Route
import com.vaadin.flow.server.VaadinRequest
import org.slf4j.LoggerFactory

@Route(value = "myform")
@JavaScript("https://somejavascript.js")
class MyForm : Div() {
    private val logger = LoggerFactory.getLogger(this::class.java)

    init {
        val request = VaadinRequest.getCurrent()
        val myparam= request.getParameter("myparam")
        logger.info("myparam = $myparam")

    }
}

As you can see I import script: somejavascript.js by annotation. Nice. This script has method : myJavaScriptMethod How I can call this method in my Vaadin class MyForm ?

1

There are 1 answers

9
kscherrer On

You can call your JS function myJavaScriptMethod() with this code:

UI.getCurrent().getPage().executeJavaScript("myJavaScriptMethod();");

You can find the documentation here
(strangely this documentation is only shown for the vaadin version 11, but it's still done the same way in Vaadin 12 and 13)