google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






Running JavaScript from a Java Applet Although having very similar names, Java and JavaScript are totally two different languages separately but they are able to create the web-based applications. JavaScript is supported by all browsers, but Java is not. Despite of being two different programming languages but their applications are able to communicate with each other, this free HTML JavaScript tutorial/Java applet tutorial guides you how to call JavaScript in Java applet, how to build the Java applet call JavaScript through a simple JavaScript example code.


Free iPage Web Hosting for First Year NOW



If you're still looking for a reliable web host provider with affordable rates, why you don't take a little of time to try iPage, only with $1.89/month, included $500+ Free Extra Credits for the payment of 24 months ($45)?

Over 1,000,000+ existisng customers can not be wrong, definitely you're not, too! More important, when you register the web hosting at iPage through our link, we're going to be happy for resending a full refund to you. That's awesome! You should try iPage web hosting for FREE now! And contact us for anything you need to know about iPage.
Try iPage for FREE First Year NOW

While creating the UTM Coordinate Converter applet, I was looking for examples on how to get a Java Applet, to call a Javascript function in the html. I found various bits of information and some examples (that didn't always work), and so decided I would pool the information and try to give a clear tutorial on how to do this. This tutorial assumes Java6 but should work for Java5 as well. I'm not too sure about earlier version of Java.

Step 1: Get plugin.jar

plugin.jar contains the classes needed for an applet to call javascript functions. It is necessary to have plugin.jar in the classpath of your development environment. Plugin.jar can be found in /lib directory of your java installation (example on Windows: C:\Program Files\Java\jre6\lib). It is not necessary to specify plugin.jar in the classpath when putting your applet on your webpage, since the Java plugin will already have plugin.jar (see example in Step 5)

Step 2: import required classes

In the Applet Code add the following import:

Code:

import netscape.javascript.JSObject;

Don't worry about 'netscape' in the package name, it works for IE just fine.

Step 3: create your javascript function for your webpage:

Code:

<script type="text/javascript">
  //Javascript function
  function testFunction(param1, param2) {
    alert('Called by applet: ' + param1 + param2);
  }
</script>

In this example, the javascript just displays an alert box, but it could do anything you want. This function also accepts parameters, to show how parameters can be passed in. Note: the javascript can either be included directly in the HTML from a separate .js file.

Step 4: Add a call to the javascript in the applet java code

Code:

private void callJavascriptFunction(){
  try {
    JSObject window = JSObject.getWindow(this);
    String param1 = "Hello from ";
    String param2 = "a Java applet";
    Object[] params = {param1, param2};
    window.call("testFunction", params);
  } catch (Exception e) {
    e.printStackTrace();
  }
}

This Java function creates a JSObject object called window. It then sets up a couple of parameters (as Strings) and puts them into an Object array. It finally calls thejavascript function created earlier using the call() method and passes in the name of the javascript function, and the params array.

Step 5: Add applet to html page

Below is the way I add applets to HTML pages, which seems to work across all browsers. The key for the javascript to work is to include the param name="MAYSCRIPT value="true"

Code:

<!--[if !IE]>-->
<object classid="java:CoordinateConverterApplet.class"
    type="application/x-java-applet"
    codebase="../assets/applets"
    archive="CoordinateConverter.jar"
    width="450" height="760" >
    <!-- Konqueror browser needs the following param -->
    <param name="archive" value="CoordinateConverter.jar" />
    <param name="MAYSCRIPT" value="true" />
    <p>Coordinate Converter Applet</p>
<!--<![endif]-->
  <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
      width="450" height="760" >
    <param name="code" value="CoordinateConverterApplet" />
    <param name="codebase" value="../assets/applets/" />
    <param name="archive" value="CoordinateConverter.jar" />
    <param name="MAYSCRIPT" value="true" />
    <p>Coordinate Converter Applet</p>
  </object>
  <!--[if !IE]>-->
</object>
<!--<![endif]-->

Step 6: Try it out!

In your Java Applet code, when you make a call to callJavaScriptFunction(), it will call the Javascript function, and if everything works correctly, it will display an alert box as follows:

Java Script Alert

To see a live working example of calling javascript from an applet, you can go to the UTM Coordinate Converter tool. In this tool, when clicking on a Convert button, the applet calls a Javascript function, passing in the coordinate, and the javascript updates the Google Map.

Link to JSObject API documentation: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/LiveConnect/JSObject#getWindow

AIVideo-App.com
Generate your business videos by AI with voice or just text

chatGPTaz.com
Talk to ChatGPT by your mother language

AppAIVideo
Your first FREE AI Video App

Deepfake Video
Deepfake AI Video Maker

Deepfake
Deepfake AI Video Maker

AI Deep Fake
Deepfake AI Video Maker

AIvidio
AI Video Mobile Solutions

AIvideos
AI Video Platform & Solutions

AIvedio
AI Video App Maker

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Faceswap AI Online
Swap Faces Video, Photo & GIFs Instantly with Powerful AI Tools - Faceswap AI Online FREE

Temu Free $500 for New Users
Claim Free Temu $500 Credit via Affiliate & Influencer Program

Free TikTok Ads Credit
Master TikTok Ads for Your Business Marketing

Dall-E-OpenAI.com
Generate creative images automatically with AI

chatGPT4.win
Talk to ChatGPT by your mother language

First AI Product from Elon Musk - Grok/UN.com
Speak to Grok AI Chatbot with Your Language

Tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

iPhoneKer.com
Save up to 630$ when buy new iPhone 16

Buy Tesla Optimus Robot
Order Your Tesla Bot: Optimus Gen 2 Robot Today for less than $20k

JavaScript by day


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web