Share this page 

Interaction without LiveConnectTag(s): Javascript interaction


Java-Javascript interaction may not be possible with old browsers or platforms.

Java to Javascript
One way to pass information from Java to Javascript, without LiveConnect or scripting support, is to have an hidden frame acting as a bridge.

From Java you do a showDocument() with a particuliar page passing the message as parameter to the hidden frame, something like showDocument("mypage.html?HelloJavascriptFromJava", "hiddenframe").

The mypage.html page contains a javascript script to extract the parameter received in the URL (using the document.search property). And then the parameter extracted is send to another javascript function in the visible frame.

First, define the frameset with an invisible frame.

[SimpleJ2JS.HTML]

<FRAMESET ROWS="100%,*">
   <FRAME NAME="mainFrame" SRC="visiblepage.html" border=0>
   <FRAME NAME="scriptFrame"  SRC="invisiblepage.html" border=0>
</FRAMESET>

visiblepage.html contains the Applet and a javascript function. The idea is to type something in a TextField, press a Button to send a string to a javascript to do an alert() with the TextField content.

[visiblepage.html]

<HTML><HEAD>
<SCRIPT>
function showMessage(s) {
   alert(s)
   }
</SCRIPT>
</HEAD><BODY><H1>Simple Java to Javascript interaction</H1><P>
<APPLET CODE=SimpleApplet.class 
        WIDTH=150 
        HEIGHT=150>
</APPLET></BODY></HTML>

invisiblepage.html contains the function to extract the parameter and call the showMessage() function in the visible frame.

<HTML><HEAD>
<SCRIPT>
function replace(s, t, u) {
   i = s.indexOf(t);
   r = "";
   if (i == -1) return s;
   r += s.substring(0,i) + u;
   if ( i + t.length < s.length)
     r += replace(s.substring(i + t.length, s.length), t, u);
   return r;
   }
  
function getAndSendMessage() {
   theMessage = document.location.search.substring(1,255)
   if (theMessage.length > 0) {
      // replace all '+" by space
      theMessage = replace(theMessage, '+', ' ')
   
      window.parent.mainFrame.showMessage(unescape(theMessage))
      }
   }
</SCRIPT>
</HEAD><BODY onLoad="getAndSendMessage();">
</BODY></HTML>
and finally the Applet (a JDK102 style, since we want to be friendly to older browsers).
[SimpleApplet.java]
import java.applet.Applet;
import java.awt.*;

public class SimpleApplet extends Applet {
 TextField aMessage;
 Button    sendButton;

 public void init() {
   aMessage = new TextField(20);
   add(aMessage);
   sendButton = new Button("Send to Javascript");
   add(sendButton);
   }

 public boolean action(Event e, Object o) {
   if (e.target.equals(sendButton)) {
     try {
       getAppletContext().showDocument
        (new java.net.URL
           (getCodeBase(),
            "invisiblepage.html?"+ 
            java.net.URLEncoder.encode(aMessage.getText())), 
         "scriptFrame");
       }
     catch (Exception ex) {
       ex.printStackTrace();
       }
     }
   return true;
   }
 } 
Try it here.

Javascript to Java
The idea here is to have an invisible Applet in an invisible frame that will receive a message (from Javascript in the visible frame) through the search part of its URL. Then via a static pointer to the visible Applet, the invisible Applet pass the message to the visible Applet by calling the appropriate function.

First, the Frames definition

[SimpleJS2J.html]

<FRAMESET ROWS="100%,*">
   <FRAME NAME="visibleFrame" SRC="visiblepage2.html" border=0>
   <FRAME NAME="invisibleFrame"  SRC="invisiblepage2.html" border=0>
</FRAMESET>

The visible page

[visiblepage2.html]

<HTML><HEAD>
<SCRIPT>
 function send2Java() {
    // you may need to encode the value with the escape() function
    parent.invisibleFrame.location =
       "invisiblepage2.html?" + document.forms[0].FromJs.value
    }
 </SCRIPT>
 </HEAD><BODY><H1>Simple Javascript to Java interaction</H1>
 <FORM>
   <INPUT TYPE=input NAME=FromJs WIDTH=50 VALUE="HelloWorld">
   <INPUT TYPE=button VALUE="Send to JAVA" onClick="send2Java();">
 </FORM>
  
 <APPLET CODE=SimpleApplet2.class 
         WIDTH=300 
         HEIGHT=150>
 </APPLET></BODY></HTML>

The SimpleApplet2 is TextField which will be used to display the message type in the HTML FORM.

[SimpleApplet2.java]


import java.applet.Applet;
import java.awt.*;

 public class SimpleApplet2 extends Applet {
  TextField aMessage;

  public void init() {
    add(new Label("Message from Javascript "));
    aMessage = new TextField(20);
    add(aMessage);
    // put a pointer to this Applet in a static
    // variable which can be shared with the InvisibleApplet
    SimpleAppletRegistered.sa = this;
    }

  public void setMessage(String msg) {
    aMessage.setText(msg);
    }
}

The class to hold a static pointer to SimpleApplet2. This pointer will be used by the InvisibleApplet.

[SimpleAppletRegistered.java]

public class SimpleAppletRegistered {
  static SimpleApplet2 sa;
} 
And finally the invisible page and InvisibleApplet

[invisiblepage2.html]

<HTML><HEAD>
</HEAD>
<BODY>
<APPLET CODE=InvisibleApplet.class 
         WIDTH=1 
         HEIGHT=1>
 </APPLET>
</BODY></HTML>

[InvisibleApplet.java]


import java.applet.Applet;

public class InvisibleApplet extends Applet {
  public void init() {
    String completeURL = getDocumentBase().toString();
    System.out.println("URL received : " + completeURL);
    int i = completeURL.indexOf("?");
    if (i > -1) {
       String msg = completeURL.substring(completeURL.indexOf("?") + 1);
       // call SimpleApplet via the static pointer
       // you may to decode the string here with
       //   java.net.URLDecoder.decode() method.
       SimpleAppletRegistered.sa.setMessage(msg); 
       }
    }
}
Try it here.

NOTE: A Static class can be shared between Applets only if the Applets are coming from the same server and the MAYSCRIPT is not used.

NOTE: To send a message containing spaces and other special characters like & or ?, you will need to encode the message from Javascript (with the escape function) and decode the message in Java.

NOTE : With IE, you need to run this sample through a Web server.