IT • archiv

rus / eng | Логин | Добавить отзыв | Печать | Почта | Клуб


LiveConnect: руководство для IE

( Walk )

2 ways to call JS functions from applets:

  1. Netscape's LiveConnect
  2. JavaBeans' outgoing events

The 1st way should work in both IE and NS and the JavaPlugin from Sun, the second only works in IE since it relies on JavaCOM.

  1. Use the call() or eval() methods in netscape.javascript.JSObject, which is documented in the LiveConnect section on the NS manuals on JS: manuals/javascript.html (the relevant pages are attached)
  2. This is tricky and almost undocumented and works this way: a Java class works like an ActiveX control in IE and IE knows how to manage it relying on JavaBean design patterns (with a few modifications), that's to say:
    • only public methods are used and no polymorphism is supported
    • to access properties you must supply getX() and/or setX() methods (getX() for readable props, setX() for writable ones, both for R/W props) (thus you can either use the access methods or the property from JS)
    • values are converted on the fly: JS strings to String, JS numbers to int, float... or whatever, JS objects to any Java object indicated, Java objects are wrapped in a COM Variant to old their dispatch interface
    • the bean class can generate events implementing a pair of methods addXListener(XListener) and removeXListener(XListener); you have to define an interface XListener (of course you can use the ones in java.awt.event.* if they suits your need) which extends java.util.EventListener; the abstract methods in the interface should return void and accept an XEvent as parameter (which extends java.util.EventObject) Once you have setup those 3 beans, you use them this way: v embed the applet (well, it needs not be an Applet actually, it can be any java.awt.Component — if it must be rendered graphically — or any class if no display is needed) using the <OBJECT> tag and the "java:" moniker:
      <OBJECT ID="xappid"
    • set the CSS display property to "hidden" if no visual rendering is needed and the WIDTH and HEIGHT attributes/properties; remeber to give it an ID if you want to easily access the bean from JS; remember to put XApplet.class in the classpath of the browser or in CODEBASE and use the full name if it's in a package other than the empty one (and in such case take into account any needed directory tree: that is, create a mypkg dir and put the .class there); know that IE uses "." as default CODEBASE
    • instruct IE to setup an XListener:
      <SCRIPT LANGIAGE="JScript"
      FOR="xappid" EVENT="someXEvent(e)">
      // function code here: global code, not function() {}
      // the var "e" is an XEvent object
      you do not have to create an event handler for each method in XListener, only the one you need: IE will implement stub methods for the others
    • raise events in you bean whenever you need:
         synchronized(xlisteners) {
              for(int i = 0; i < xlisteners.size(); i++) {
                  XEvent e = new XEvent();
      this assumes that you have somewhere
      private Vector xlisteners = new Vector();
      public void addXListener(XListener xl) {
          synchronized(xlisteners) {
      public void removeXListener(XListener xl) {
          synchronized(xlisteners) {

And this is the whole story. Most likely you need not the complexites (but the power!) of JavaCOM in IE and JSObject is enough.

Справка | Условия Copyright © 1999 — 2010, IT • archiv.
В начало | Логин | Добавить отзыв | Поиск | Почта