https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Ray Rahke

03/16/2024, 9:03 AM
I can't figure out how Kotlin's multiplatform for JS wants me to invoke
window.onload()
. the documentation is either hard to find or nonexistant
j

Joffrey

03/16/2024, 9:36 AM
Is this function listed in JS at all? Could you please link to the W3C docs or other that mentions that
window.load()
should exist? All I see is the load event and thus the
window.onload
event listener that you can assign:
Copy code
import kotlinx.browser.*

fun main() {
    window.onload = { event -> 
        println("Received event $event")
    }
}
r

Ray Rahke

03/16/2024, 5:10 PM
no I meant onload
but I also could not find anything usefu on the docs for onload
i was getting
Type mismatch: inferred type is () -> Unit but Event was expected
Copy code
window.onload?.invoke() { event ->
        print("loaded")
    }
Copy code
e: Type mismatch: inferred type is ([Error type: Cannot infer a lambda parameter type]) -> Unit but Event was expected
e: Cannot infer a type for this parameter. Please specify it explicitly.
annotating it as
event: Event ->
just gives me
Copy code
Unresolved reference: Event
r

Robert Jaros

03/16/2024, 5:34 PM
you can always do
event: dynamic
r

Ray Rahke

03/16/2024, 5:37 PM
that got rid of one of the errors
Copy code
import kotlinx.browser.*
import org.w3c.dom.*


fun main() {
    val kotlin = "🙂"
    window.addEventListener("load") { event: dynamic ->
        print("hello")
    }
    println(kotlin)
}
Copy code
None of the following functions can be called with the arguments supplied: public final fun addEventListener(type: String, callback: ((Event) -> Unit)?, options: dynamic = ...): Unit defined in org.w3c.dom.Window public final fun addEventListener(type: String, callback: EventListener?, options: dynamic = ...): Unit defined in org.w3c.dom.Window
oh typo, the lambda should be a second arugment
3 Views