开发者

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 开发者_StackOverflow社区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).


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜