最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - Passing JS function to applet for as event listener - Stack Overflow

programmeradmin1浏览0评论

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 pressed it will call the passed js callback

function onCommand() {
  alert('Button pressed from applet');
}
applet.onCommand(onCommand);

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 pressed it will call the passed js callback

function onCommand() {
  alert('Button pressed from applet');
}
applet.onCommand(onCommand);
Share Improve this question edited Sep 20, 2017 at 22:36 P.S. 16.4k14 gold badges65 silver badges86 bronze badges asked Sep 24, 2008 at 11:38 kenken 5,0864 gold badges32 silver badges26 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

I tend to use something I derived from the reflection example at the bottom of this page, as then you don't need to meddle with your classpath to get it to pile

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

You can use JSObject to call back into javascript from Java.

From that 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
     }
}

ps. to use JSObject you may need to include "MAYSCRIPT" tag to applet html tag.

发布评论

评论列表(0)

  1. 暂无评论