0

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?

2 Answers 2

1

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

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

1 Comment

Graphics g = getGraphics(); That's not the correct way to do custom rendering in AWT or Swing.
0

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!

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.