pablosd
pablosd

Reputation: 352

What is the simplest way to send data from JApplet to JavaScript?

I'm having a HTML page with an applet that gathers data and then is supposed to send this data through JavaScript call, JS sends it to PHP file.

I can send the arguments and values by GET method, but is there something better? Can I use JSON for example in this applet? How do I send it to JavaScript?

The applet should not be signed, so I can't add anything fancy there or can I?

Thanks for any tips/pointers.

Edit:
I should have probably put it in some other words: "What's the most universal way of sending data (>2kb) from JApplet to JavaScript?" I'd like it to run on as many browsers as possible (I'm aware that some people don't even have java installed).

Upvotes: 3

Views: 1353

Answers (3)

Rick Brunken
Rick Brunken

Reputation: 21

As of JRE 1.6.27 and higher IE 6+ (don't know if it is IE only) will get focus everytime a function is executed through the getAppletContext method. If you do not want this you're better of using JSObject.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168825

What is the simplest way to send data from Java JApplet to Javascript?

Probably this:

applet.getAppletContext().showDocument
    (new URL("javascript:someJavaScriptFunction(" + params + ");"));

This technique I saw recently on Real's How To under Call Javascript from a Java applet. That site is always my first place to check for tasks to do with Java/JS interaction. Besides that technique, other ways of passing data Java<->JavaScript are detailed and (for the most part) demonstrated.

I just posted one 'simple' way.

Upvotes: 4

Bozho
Bozho

Reputation: 597114

You can invoke javascript functions, and in general communicate with javascript through netscape.javascript.JSObject. See here. An example:

JSObject win = JSObject.getWindow(this);
win.call("receiveDataFromApplet", new Object[] {param1, param2, param3});

This will call the desired function with the given arguments.

JSObject is part of JRE/plugin.jar, so it will run on every JRE. But you will have to add it to your classpath (in your IDE) in order to compile the applet.

Upvotes: 3

Related Questions