Rose Wangui
Rose Wangui

Reputation: 21

How to send JavaScript variables to Java applet

I have a JavaScript function which is:

function printreceipt(tax,subtotal,total)
{
subtotalElem=document.getElementById("total");
var taxElem=document.getElementById("tax");
productElem=document.getElementById("product-name");
alert("here are my products" + taxElem.innerHTML + subtotalElem.innerHTML);
}

How to send JavaScript variables to Java applet?

Upvotes: 1

Views: 5518

Answers (3)

grunk
grunk

Reputation: 14938

Your applet should have a public method , for example receiveData() :

public void receiveData(String dataFromJS)
{
   //Do what you need with your data
}

Let's say you have something like that in your web page :

<applet id ="appletID" name="myApplet" ... ></applet>

In your javascript you just have to call the applet public method like this :

var myApp = document.applets['myApplet'];
myApp.receiveData(taxElem.innerHTML + subtotalElem.innerHTML);

The previous example will send the taxElem and subtotalElem content to the applet.

To send data from Applet to JS you sould use JSObject in your applet

Upvotes: 5

Andrew Thompson
Andrew Thompson

Reputation: 168845

The most reliable way to do this is to open a new page with the parameters encoded in the URL, then write those parameters as into an applet element as param elements. (Though +1 to each of the other answers.)

Upvotes: 0

Rostislav Matl
Rostislav Matl

Reputation: 4543

You can use netscape.javascript.JSObject for Java-to-JS direction or reference the applet by its id for JS-to-Java.

See here for detailed example : http://rostislav-matl.blogspot.com/2011/10/java-applets-building-with-maven.html

Upvotes: 2

Related Questions