Results 1 to 6 of 6

Thread: How to invoke JavaScript Code from an Applet?

  1. #1
    Join Date
    Aug 2006
    Posts
    181

    How to invoke JavaScript Code from an Applet?

    Hi friends,
    I have done some basic coding in Java. Before that I have done C++, JavaScript and HTML programming language. Now recently I have started doing the Applets in Java. So I am not having much knowledge about an Applets. Now I want to invoke the Javascript from applet. So I thought to post you guys for help.!! Please tell me how to invoke JavaScript Code from an Applet? Expecting the reply sooner.
    QTechnology Ultra-Quiet ATX PSU 460W I MSI K8N Neo4-F I AMD Opteron 144 CABNE0545 2.66Ghz I 2 x 512MB RAM I 380GB Maxtor SATAI Raid0 Hard Drive I 40GB Maxtor IDE Hard Drive I Nvidia GeForce 7900GTX I Win XP Home Service Pack 2 I Soundblaster Xtreme Fidelity Fatal1ty I Mercury Home Theater HT-4500

  2. #2
    Join Date
    Mar 2008
    Posts
    349

    Re: How to invoke JavaScript Code from an Applet?

    Invoking the JavaScript code from an Applet is not tough task. Since, you are new that's why you don't know about it. Otherwise, I am sure that looking once you can understand to invoke the coding of JavaScript from Applet. But you should know some basic things first so that there would be no doubts while doing the coding. You should know that an Applets can invoke JavaScript functions present in the same web page as the applet. The class that enables applets to retrieve a reference to JavaScript objects and interact with the web page is the netscape.javascript.JSObject class.

  3. #3
    Join Date
    Nov 2008
    Posts
    1,192

    Re: How to invoke JavaScript Code from an Applet?

    A Javascript function is called from Java by using the showDocument method. A "javascript:" as the protocol is needed as a URL. The following code may help you in understanding the applets :
    Code for Java applet :
    Code:
    import java.applet.*;
    import java.net.*;
    
    public class JavaEx extends Applet{
      public void init(){
        String msg = "Hello from Java (using javascript alert)";
        try {
          getAppletContext().showDocument
            (new URL("javascript:doAlert(\"" + msg +"\")"));
          }
        catch (MalformedURLException me) { }
      }
    }
    Code for Javascript and HTML :

    HTML Code:
    <HTML><HEAD></HEAD><BODY>
    <SCRIPT>
    function doAlert(s) {
       alert(s);
       }
    
    </SCRIPT>
    <APPLET CODE="JavaEx.class"
            NAME="myApplet"  MAYSCRIPT
            HEIGHT=15 WIDTH=15>
    </APPLET>
    </BODY>
    </HTML>

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to invoke JavaScript Code from an Applet?

    I am providing an example so that you can get the concepts more properly. In the following example of coding, you type in the TextField a Javascript function and press the button to execute the function. Here is the coding for that :
    Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    import netscape.javascript.*;
    
    public class JavaDemo extends Applet implements ActionListener {
      Button b;
      TextField tf;
    
      public void init() {
         tf = new TextField(25);
         add(tf);
         b = new Button("execute Javascript");
         add(b);
         b.addActionListener(this);
         }
    
      public void actionPerformed(ActionEvent ae) {
         if (ae.getSource() == b) {
           JSObject win = (JSObject) JSObject.getWindow(this);
           win.eval(t.getText());
           }
         }
    }

  5. #5
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to invoke JavaScript Code from an Applet?

    There is an another way to use the Reflection API. But this is very difficult to understand and is lengthy too. In this way you don't need to modify your CLASSPATH for compilation or even import the netscape.jsobject package. The following coding demonstrates the same :
    Code:
    import java.awt.event.*;
    import netscape.javascript.*;
    import java.lang.reflect.*;
    ...
    // This goes in Applet Class
    ...
    String jscmd = "window.close()";  
    String jsrst = null;
    boolean success = false;
    try {
      Method getw = null, eval = null;
      Object jswin = null;
      Class c =
        Class.forName("netscape.javascript.JSObject");
      Method ms[] = c.getMethods();
      for (int i = 0; i < ms.length; i++) {
          if (ms[i].getName().compareTo("getWindow") == 0)
             getw = ms[i];
          else if (ms[i].getName().compareTo("eval") == 0)
             eval = ms[i];
          }
      }
      Object a[] = new Object[1];
      a[0] = this;             
      jswin = getw.invoke(c, a); 
      a[0] = jscmd;
      Object result = eval.invoke(jswin, a);
      if (result instanceof String)
        jsrst = (String) result;
      else
        jsrst = result.toString();
      success = true;
      }
    
    catch (InvocationTargetException ite) {
      jsresult = "" + ite.getTargetException();
      }
    catch (Exception e) {
      jsresult = "" + e;
      }
    
    if (success)
        System.out.println("eval succeeded, result is " + jsresult);
    else
        System.out.println("eval failed with error " + jsresult);

  6. #6
    Join Date
    May 2008
    Posts
    2,297

    Re: How to invoke JavaScript Code from an Applet?

    In order to call javascripts we need to get hold of a JSObject. But the most important thing that you need to do first, is to import the netscape.javascript.JSObject into our class. The given code explains the same :
    Code:
    public void init()
        {
            jso = JSObject.getWindow(this);
        }
    Now you need to add the JButton to our applet. Then you will have to add an ActionListener to the JButton, and it's actionPerformed method calls the javascript. The following code does the same :
    Code:
    public void actionPerformed(ActionEvent ae) {
                    if(jso != null )
                        try {
                            jso.call("updateWebPage", new String[] {txt.getText()});
                        }
                        catch (Exception exp) {
                            exp.printStackTrace();
                        }
            }

Similar Threads

  1. Need JavaScript Code to open URL
    By AM-Anthony in forum Software Development
    Replies: 2
    Last Post: 12-04-2012, 06:50 PM
  2. How to invoke methods of C # in Silverlight using javascript
    By Tynan in forum Software Development
    Replies: 4
    Last Post: 27-02-2011, 08:58 AM
  3. Java code for video applet
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 09-03-2010, 11:24 AM
  4. Javascript code in servlet
    By Aaliya Seth in forum Software Development
    Replies: 5
    Last Post: 14-01-2010, 07:02 PM
  5. How to convert Javascript code to Html code
    By Bosch in forum Software Development
    Replies: 3
    Last Post: 21-09-2009, 01:20 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,561,061.23670 seconds with 16 queries