# Calling javascript from kotlin

**URL:** <https://discuss.kotlinlang.org/t/calling-javascript-from-kotlin/688>\
**Category:** Uncategorized\
**Created:** [December 3, 2013, 8:40pm UTC](https://discuss.kotlinlang.org/t/calling-javascript-from-kotlin/688 "2013-12-03T20:40:14Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rnentjes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnentjes/32/1461_2.png) [@rnentjes](https://discuss.kotlinlang.org/u/rnentjes)\
**Post date:** [December 3, 2013, 8:40pm UTC](https://discuss.kotlinlang.org/t/calling-javascript-from-kotlin/688/1 "2013-12-03T20:40:14Z")

</div>

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() {  
&nbsp;&nbsp;var script = window.document.createElement(“script”);

&nbsp;&nbsp;script.textContent = “callJavascriptTest();”  
&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![rnentjes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rnentjes/32/1461_2.png) [@rnentjes](https://discuss.kotlinlang.org/u/rnentjes)\
**Post date:** [December 3, 2013, 9:13pm UTC](https://discuss.kotlinlang.org/t/calling-javascript-from-kotlin/688/2 "2013-12-03T21:13:46Z")

</div>

Ok, I found another method that works:

&nbsp;&nbsp;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

---

<div class="post-metadata">

**Author:** ![bashor](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/bashor/32/1431_2.png) [@bashor](https://discuss.kotlinlang.org/u/bashor)\
**Post date:** [December 4, 2013, 11:54am UTC](https://discuss.kotlinlang.org/t/calling-javascript-from-kotlin/688/3 "2013-12-04T11:54:55Z")

</div>

Hi,

You can write something like:

```kotlin
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](http://kotlin-demo.jetbrains.com/?publicLink=113810345976161206390-388905040) from [box2d.js](https://github.com/kripken/box2d.js/)

[\<kotlin-sources\>](https://github.com/JetBrains/kotlin/)  
&nbsp;&nbsp;[/js/js.libraries/src/code/](https://github.com/JetBrains/kotlin/tree/master/js/js.libraries/src/core)dom\*  
&nbsp;&nbsp;[/js/js.libraries/src/jquery/](https://github.com/JetBrains/kotlin/tree/master/js/js.libraries/src/jquery)\*  
&nbsp;&nbsp;[/js/js.libraries/src/html5/](https://github.com/JetBrains/kotlin/tree/master/js/js.libraries/src/html5)\*  
etc.
