admin管理员组

文章数量:1336632

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?

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?

Share Improve this question edited Apr 12, 2012 at 12:04 Andrew Thompson 169k41 gold badges222 silver badges436 bronze badges asked Apr 12, 2012 at 11:41 Rose WanguiRose Wangui 211 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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

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./2011/10/java-applets-building-with-maven.html

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

本文标签: How to send JavaScript variables to Java appletStack Overflow