1

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?

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

6 Comments

the content is not shown as string.how can the content be shown as string in the above case.
how should i pass the data like the tax to the parameter passed datafromJS
Simply call the method. If you do myApp.receiveData('this my data'); The parameter dataFromJs in your applet will contain the string used in JS. String is not mandatory , you can use int or whatever you need
means the parameter dataFromJS will contain string which is in the "eg " taxElem.innerHTML.
will i add more parameters or the 1 above will display the whole string
|
2

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

Comments

0

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.)

1 Comment

the applet is not receiving the informatio as a string.how can i do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.