Calling javascript from kotlin


#1

Hi,

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();”
  window.document.body.appendChild(script);
}

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?

Thanks!

Rien


Questions about the Javascript API
Kotlin and Meteor JS
#2

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.

Rien


#3

Hi,

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() {
     callJavascriptTest()
     val s = callJavascriptTest2(1)
     val f = Foo(s)
     val i = f.bar
}


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

<kotlin-sources>
   /js/js.libraries/src/code/dom*
   /js/js.libraries/src/jquery/*
  /js/js.libraries/src/html5/*
etc.