2

I am trying to compile Kotlin code as Javascript. In my code I need to encode string as URI. My 2 variants are both fail compilation:

class PlaceholderJS(prefix: String, placeholder: String?): Placeholder(prefix, placeholder) {
override fun encode(str: String): String {
    return encodeURIComponent(str)
}

In this code compiler cannot find function encodeURIComponent(str), which according to https://www.w3schools.com/jsref/jsref_encodeuricomponent.asp is supported by all browsers.

Alternative:

class PlaceholderJS(prefix: String, placeholder: String?): Placeholder(prefix, placeholder) {
override fun encode(str: String): String {
    return URLEncoder.encode(str, Charsets.UTF_8.name())
} 

cannot find Java class URLEncoder (imported in the file as in Java). This works when compiled for JVM, but not JS.

Also I have Kotlin module marked with:

compileKotlin2Js.kotlinOptions.moduleKind = "umd"

1 Answer 1

4

I found that latest way to declare Javascript function in Kotlin is:

external fun encodeURIComponent(str: String): String

Once added it to the Kotlin class everything get compiled without a problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.