Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call existing JavaScript function from Java method using CodeName One?

I have an HTML file with a simple JavaScript function, and I want to call this existing JavaScript function from my Java method using CodeName One library.

<!DOCTYPE html>

<html>
<head>
    <title>who cares?</title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script>
        function setName(name)
        {
            document.getElementById("nameLabel").innerHTML = name;
        }
    </script>
</head>
<body>
    <div>
        peace be upon you, you are welcomed :  <span id="nameLabel"
                                                     style="color: blue;"></span>
    </div>
</body>
</html>

So my question is how to call setName(name) function from Java code using CodeName One.

Although I found related questions and answers such as one this but I didn't find what I want!

like image 530
Anas Avatar asked Jul 26 '26 05:07

Anas


1 Answers

Finally I found it

JavascriptContext context;
//initialization of [context] and loading HTML 
JSObject window = context.getWindow();
window.call("setName", new Object[]
{
      "Moslim"
});
like image 139
Anas Avatar answered Jul 27 '26 20:07

Anas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!