Ananthi Thangavel
Ananthi Thangavel

Reputation: 91

How to call java method from javascript?

I have one html file with applet class and one java file in same directory. While I am calling java method from javascript using applet I got an error like:

applet method is not a function

How to call java method from javascript?

Upvotes: 0

Views: 870

Answers (2)

vlouccer 19
vlouccer 19

Reputation: 1

I have a technique to call javascript from java or java from javascript. Do this on web:

<a href="file:///android_asset/YourFile.html?q=true"></a>

And do this on java:

webview1.loadUrl("file:///android_asset/YourFile.html");
new Timer().scheduleAtFixedRate(new TimerTask() {
    @Override
    public void run() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (webview1.getUrl().contains("?q=true")) {
                    webview1.loadUrl("file:///android_asset/YourFile.html");
                    
// YOUR CODE HERE
                }
            }
        });
    }
}, 20, 20);

How it works? Web can change the URL easily. (i use "?q=true") and java can get the webview's URL. every 20ms, java get the url and check if the url contains "?q=true" then run the code. you can also call a javascript function using java by reversing the code!

Upvotes: 0

Phạm Hợp
Phạm Hợp

Reputation: 21

If your applet name is "myApp".

Then you have this method inside it for example,

public void hi() {
Graphics g = getGraphics();
g.drawString("wads up", 10, 10);}

You can call

<INPUT type="button" value="call method"    
onClick = "document.appName.hi()">

Update

if you wish to accept parameters in your app, you need to specify this in your applet codes:

String para = this.getParameter("fromPage");

and you can have

<PARAM name="fromPage" value="Param Sent to Applet!">

from within the applet tags to pass it to the applet

Upvotes: 1

Related Questions