Passing JS function to event listener applet

Is it possible to pass a function / callback from javascript to a java applet?

For example, I have an applet with a button that, when clicked, will call the passed js callback

function onCommand() { alert('Button pressed from applet'); } applet.onCommand(onCommand); 
+4
source share
3 answers

I use what I got from the reflection example at the bottom of this page , since then you do not need to interfere with your classpath to force it to compile

Then I just pass the JSON strings between the applet and javascript

+4
source

You can use JSObject to call back in javascript with Java.

From this page:

 import netscape.javascript.*; import java.applet.*; import java.awt.*; class MyApplet extends Applet { public void init() { JSObject win = JSObject.getWindow(this); JSObject doc = (JSObject) win.getMember("document"); JSObject loc = (JSObject) doc.getMember("location"); String s = (String) loc.getMember("href"); // document.location.href win.call("f", null); // Call f() in HTML page } } 
+3
source

ps. To use JSObject, you may need to include the "MAYSCRIPT" tag in the html tag of the applet.

+3
source

Source: https://habr.com/ru/post/1277109/


All Articles