JavaScript Interop


Kweb's DOM interaction functionality is build on top of two functions that allow you to interact directly with the browser's JavaScript interpreter:

Note that this is unrelated to Kotlin's ability to compile to JavaScript.

Calling a JavaScript function

doc.body {
    browser.callJsFunction("""alert("Hello World!")""")

Calling with parameters

You can pass parameters to a JavaScript function by passing them as arguments to the callJsFunction() function, using {} for substitution:

doc.body {
    val greeting = "Hello".json
    val name = "World".json
    browser.callJsFunction("""alert({} + " " + {} + "!")""", greeting, name)

Parameters must be converted to a JsonElement, for example by using the json extension property. Within the JavaScript code the {} should be treated like a variable, so alert({}) is ok but alert("{}") will not work.

Function caching and preloading

Kotlin automatically caches JavaScript functions in the browser for efficiency. If the function is first called during initial page render, it will be parsed and cached as part of the initial page load.

Calling a JavaScript function with a result

You can also retrieve a result from a function call using callJsFunctionWithResult(). Note that the last line of the jsBody parameter must be a return statement:

doc.body {
    elementScope().launch {
        val result : JsonElement = browser.callJsFunctionWithResult("return")
        println("Result: ${result.jsonPrimitive.intOrNull}")

callJsFunctionWithResult() is a suspend function so it must be called inside a CoroutineScope. You can create one within Kweb's DSL using elementScope(). This scope will be cancelled automatically when this part of the DOM is no-longer needed.