Reprator
08/07/2025, 9:11 AMfun createScriptOnErrorHandler(url: String): ContextFunction<Void, Tuple1<js.core.JsAny>, Tuple1<js.core.JsAny?>> {
return ContextFunction(
"event", body = """
try {
const errorMessage = "Error loading script: $url. Message: " + event.message +
", Source: " + event.filename +
", Line: " + event.lineno +
", Col: " + event.colno +
", Error: " + event.error;
console.error(errorMessage);
if (typeof window !== 'undefined' && typeof window.onScriptErrorCallback === 'function') {
window.onScriptErrorCallback(errorMessage);
}
} catch (e) {
console.error("Exception in error handler: ", e);
}
return null;
""".trimIndent()
)
}
suspend fun loadJsScript(url: String, id: String) {
suspendCoroutine<Unit> { continuation ->
val script = document.createElement("script") as HTMLScriptElement
script.src = url
script.id = appCreateElement(id)
script.type = "application/javascript"
script.onerror = createScriptOnErrorHandler(url)
script.onload = EventHandler { _: Event ->
println("Script loaded: $url")
continuation.resume(Unit)
}
document.head.appendChild(script)
}
}
Tóth István Zoltán
08/07/2025, 9:40 AMonerror
is called only when the browser is not able to load "something" from the server.
For example:
• it is NOT called if the script does not exists but the server returns with something, let's say index.html
instead
• it also does NOT work if the script has an error that prevents compilation
This code worked for me (it does not handle the two points above):
suspend fun loadJsScript(url: String, id: String) {
suspendCoroutine<Unit> { cont ->
val script = document.createElement("script") as HTMLScriptElement
script.src = url
script.id = "23"
script.type = "application/javascript"
script.addEventListener("error", { _: Event ->
cont.resumeWithException(RuntimeException("Failed to load script: $url"))
})
script.onload = { event: Event ->
console.log(event)
println("Script loaded: $url")
}
document.head?.appendChild(script)
}
}
Reprator
08/07/2025, 9:53 AMTóth István Zoltán
08/07/2025, 10:25 AMFor historical reasons, different arguments are passed toI've tried with window.onerror:and `element.onerror`handlers (as well as on error-typewindow.onerror
handlers).EventTarget.addEventListener
window.onerror = { message, source, lineno, colno, error ->
println("Error: $message $source $lineno $colno $error")
cont.resumeWithException(RuntimeException("Failed to load script: $url $message $source $lineno $colno $error"))
}
The result:Tóth István Zoltán
08/07/2025, 10:26 AMonload
on script is still called as there is a response from the server.
I think you need some logic to handle the different cases.Reprator
08/07/2025, 11:08 AMTóth István Zoltán
08/07/2025, 11:10 AMReprator
08/07/2025, 11:11 AMReprator
08/07/2025, 11:11 AMTóth István Zoltán
08/07/2025, 11:12 AMTóth István Zoltán
08/07/2025, 11:13 AMReprator
08/07/2025, 11:15 AMturansky
08/07/2025, 6:17 PMerrorEvent
and loadEvent
in this case:
suspend fun loadJsScript(url: String, id: String) {
suspendCoroutine<Unit> { cont ->
val script = document.createElement(HtmlTagName.script)
script.src = url
script.id = "23"
script.type = "application/javascript"
script.errorEvent.addHandler {
cont.resumeWithException(RuntimeException("Failed to load script: $url"))
}
script.loadEvent.addHandler {
console.log(event)
println("Script loaded: $url")
}
document.head?.appendChild(script)
}
}
turansky
08/07/2025, 6:20 PMwindor.onerror
signature is weird (for historical reasons) and it's better to use more strict accessorsReprator
08/08/2025, 3:48 AMReprator
08/08/2025, 9:33 AMsuspend fun loadCss(url: String, id: String) {
suspendCoroutine<Unit> { continuation ->
val link = document.createElement("link") as HTMLLinkElement
link.rel = "stylesheet"
link.href = url
link.id = appCreateElement(id)
link.type = "text/css"
/*link.errorEvent.addHandler {
continuation.resumeWithException(RuntimeException("Failed to load script: $url"))
}*/
link.loadEvent.addHandler {
println("CSS loaded: $url")
continuation.resume(Unit)
}
document.head.appendChild(link)
}
}
is getting called for other error as well, not specifict to this script, is there anything, from where i can handle specific to this script onlyturansky
08/08/2025, 11:05 AMturansky
08/08/2025, 11:07 AMTóth István Zoltán
08/08/2025, 11:08 AMTóth István Zoltán
08/08/2025, 11:09 AMturansky
08/08/2025, 12:01 PMDoes `loadEvent.addHandler`do anything special?Accessor give you knowledge, that such event is known for this target