Java <-> Javascript for non-Applets?

I'm working with an existing Java codebase which, while it can be invoked from an HTML page using an <APPLET> tag, does not actually subclass the Applet class. The same jars are also used in a non-browser context, so they did not subclass Applet.

Now I need to communicate some values from Java back to the Javascript of the invoking page. Normally one would do this using JSObject, but so far as I can one has to use JSObject.getWindow which only works for subclasses of Applet.

Is there either:

  • a way to use JSObject from something which isn't an Applet subclass?
  • some other mechanism to communicate back to the Javascript of the invoking page?

Asked by: Fiona192 | Posted: 23-01-2022

Answer 1

Call JSObject.getWindow(this) in the applet. Then pass the JSObject into the code that needs it in the usual fashion.

Answered by: Patrick333 | Posted: 24-02-2022

Answer 2

I think you're actually going to need to implement an applet in a jar file to handle the communication between your code and the browser itself. Perhaps just use the non-applet jar as a class lib and make the applet jar a simple wrapper that proxies your calls between the browser and the Java code.

Answered by: Dominik415 | Posted: 24-02-2022

Answer 3

What's wrong with returning a value?

From Javascript You can access the Java-object by using getElementById("id-of-embed-tag"). Then you can invoke any public method on that object. The Java object returned by that method will be available to your Javascript code.

Answered by: Adrian389 | Posted: 24-02-2022

Similar questions

Still can't find your answer? Check out these amazing Java communities for help...

Java Reddit Community | Java Help Reddit Community | Java Community | Java Discord | Java Programmers (Facebook) | Java developers (Facebook)