title: Javascript_Java_Interaction applet "Codebase" problem
body: <p>I saw an article about Javascript_Java_Interaction today at : </p>
  
  <pre><code> http://www.rgagnon.com/javadetails/java-0184.html
   [ You can try the working version on that site ]
   So I tried it on my PC, after some simple format change the files look like this :
  </code></pre>
  
  <p>============================================================================================</p>
  
  <pre><code>[C:/Dir_Fit/Javascript_Java_Interaction.html]
  
      &lt;FRAMESET ROWS="100%,*"&gt;
        &lt;FRAME NAME="mainFrame" SRC="Javascript_Java_Interaction_Visible_Page.html" border=0&gt;
        &lt;FRAME NAME="scriptFrame" SRC="Javascript_Java_Interaction_Invisible_Page.html" border=0&gt;
      &lt;/FRAMESET&gt;
  --------------------------------------------------------------------------------------------
  
  [C:/Dir_Fit/Javascript_Java_Interaction_Invisible_Page.html]
  
  &lt;HTML&gt;
    &lt;HEAD&gt;
      &lt;SCRIPT&gt;
        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&lt;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&gt;0)
          {
            // replace all '+" by space
            theMessage=replace(theMessage,'+',' ')
            window.parent.mainFrame.showMessage(unescape(theMessage))
          }
        }
      &lt;/SCRIPT&gt;
    &lt;/HEAD&gt;
    &lt;BODY onLoad="getAndSendMessage();"&gt;&lt;/BODY&gt;
  &lt;/HTML&gt;
  -----------------------------------------------------------------------------------------
  [C:/Dir_Fit/Javascript_Java_Interaction_Visible_Page.html]
  
  &lt;HTML&gt;
    &lt;HEAD&gt;&lt;SCRIPT&gt;function showMessage(s) { alert(s) }&lt;/SCRIPT&gt;&lt;/HEAD&gt;
    &lt;BODY&gt;
      &lt;H1&gt;Simple Java - Javascript interaction&lt;/H1&gt;&lt;P&gt;
      &lt;APPLET CODEBASE="file://C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150&gt;&lt;/APPLET&gt;
    &lt;/BODY&gt;
  &lt;/HTML&gt;
  -----------------------------------------------------------------------------------------
  Source : [C:/Dir_Fit/src/Javascript_Java_Interaction_Demo.java]
  class  : [C:/Dir_Fit/build/classes/Javascript_Java_Interaction_Demo.class]
  
  import java.applet.Applet;
  import java.awt.*;
  
  public class Javascript_Java_Interaction_Demo 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(),"Javascript_Java_Interaction_Invisible_Page.html?"+java.net.URLEncoder.encode(aMessage.getText())),"scriptFrame"); }
        catch (Exception ex) { ex.printStackTrace(); }
      }
      return true;
    }
  }
  </code></pre>
  
  <p>==========================================================================================</p>
  
  <p>It displayed the applet on the page [Javascript_Java_Interaction.html], but if I type in some text and click the button, nothing happens, the problem is in the following line :</p>
  
  <pre><code>&lt;APPLET CODEBASE="file://C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150&gt;&lt;/APPLET&gt;
  </code></pre>
  
  <p>So I copied the class file into the same dir as the html files [C:/Dir_Fit/] and changed the above line to : </p>
  
  <pre><code>&lt;APPLET CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150&gt;&lt;/APPLET&gt;
  </code></pre>
  
  <p>now it works, it will display an alert window.</p>
  
  <p>So, my question is why it didn't work when I specified the codebase ? According to Java document if the class file is in a different dir, you can tell it by codebase, the applet will show up but if you type in some text and click the button, nothing will happen, I've tried different ways to specify the code base :</p>
  
  <pre><code>    &lt;APPLET CODEBASE="file:///C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150&gt;&lt;/APPLET&gt;
  
      &lt;APPLET CODEBASE="C:/Dir_Fit/build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150&gt;&lt;/APPLET&gt;
  
      &lt;APPLET CODEBASE="build/classes/" CODE="Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150&gt;&lt;/APPLET&gt;
  
  &lt;APPLET CODE="C:/Dir_Fit/build/classes/Javascript_Java_Interaction_Demo.class" WIDTH=150 HEIGHT=150&gt;&lt;/APPLET&gt;
  </code></pre>
  
  <p>Yet, none of them works, why ? I don't want the class file to be in the same dir as the htmls. What's the fix ?</p>
  
  <p>Frank</p>