Calling javascript from kotlin



In the blog post about using Kotlin with javascript there is a hint that it should be possible to call javascript from kotlin generated javascript. Could someone give me a hint on how to do that?

I have the following working:

fun callJavascript() {
  var script = window.document.createElement(“script”);

  script.textContent = “callJavascriptTest();”

But this means a script node will be created for every call, wich can lead to a lot of nodes in the dom. Is there a way to do it directly?



Questions about the Javascript API
Kotlin and Meteor JS

Ok, I found another method that works:

  window.setTimeout(“callJavascriptTest()”, 0);

Which is better, but involves a callback and the current code will continue first, so I am still looking for a way to do a direct call.




You can write something like:

native fun callJavascriptTest(): Int = noImpl native fun callJavascriptTest2(a: Int): String = noImpl

native class Foo(param: String) {
     val bar: Int

fun test() {
     val s = callJavascriptTest2(1)
     val f = Foo(s)
     val i =

You can find additionally examples at:
b2Vec2 from box2d.js