Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Javascript to Applet communication

Status
Not open for further replies.

mojaffa

Programmer
Oct 23, 2003
8
SE
Hi
Im trying to pass a String from a javascript function to an Applet.
I cant figure out why it does not work..
It looks like this:

Script:
document.theApplet.aFunction("some text");

and Applet:
public void aFunction(String text)
{
System.out.println("Applet: got text:"+text);
repaint();
}

Then I got a Java.lang.Exception error: aFunction{0}:There is no such method.

But if I do like:
Script:
document.theApplet.aFunction();

and Applet:
public void aFunction()
{
System.out.println("Applet: got text!");
repaint();
}

Then I got the correct response: Applet: got text!
 
I don't know jack about Java, but maybe "text" is a keyword in Java.

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
Maybe try passing in a String object to the applet. Like:
Code:
document.theApplet.aFunction(new String("some text"));

Kevin
A+, Network+, MCP
 
Hi mojaffa, there are a few steps to this. First of all using ASP get the value that you want to pass in to the applet.
<%
dim s
s = Request.Form(&quot;value&quot;)
%>

Then set up a parameter within your <applet></applet> tags as such ...

<applet code=&quot;whatever.class&quot; width=xx height=xx>
<param name=&quot;str&quot; value=<%=s%>>
</applet>

Then in your java class, use the getParameter method to get the value ...

class f{
private String sValue=null;
public void init(){
s=getParameter(str);
System.out.println(str);
}

Please note this is a cut down version of what you need, but these are the steps, let me know how you get on,

Patrick
 
Im using a form to get the string. I want this string to be available to the applet when the user clicks on submit.
I havent used ASP before. First of all, where do I put the <%...%> code? Im also having the form and the javascript in one frame and the applet in another, makes it somewhat more difficult?
I tried your suggestion Kevin, but with no result.
Then I tried Patricks suggestion, which I think looks promising. But all I got was &quot;Applet:got text:NULL&quot;.
(had the <%...%> code in the header, dont know if that is the right place). Grateful for any further suggestions.
 
mojaffa, try these and let me know if it works. I'd email them but our system is down :-(

frames.asp (main page)
Code:
<html>
<frameset cols=&quot;200,*&quot;>
<frame name=&quot;f1&quot; src=&quot;test.asp&quot;>
<frame name=&quot;f2&quot; src=&quot;appletpage.asp&quot;>
</frameset>
</html>

appletpage.asp
Code:
<%@language=&quot;javascript&quot;%>
<%
	var s
	s = Request.Form(&quot;t1&quot;)
%>
<html>
<body>
<form name=&quot;frm2&quot;>
<h1>Test applet code</h1>
<applet code=&quot;test.class&quot; height=200 width=200>
<param name=str value=&quot;<%=s%>&quot;>
</applet>
<br><br><br>
Value of s = <%=s%>
</form>
</body>
</html>

test.asp
Code:
<html>
<head>
<script language=&quot;javascript&quot;>
  function check(){
    frm1.submit();
  }
</script>
</head>
<body>
<form name=&quot;frm1&quot; id=&quot;frm1&quot; method=&quot;post&quot; action=&quot;appletpage.asp&quot; target=&quot;f2&quot;>
<input type=&quot;text&quot; id=&quot;t1&quot; name=&quot;t1&quot;><br><br>
<input type=&quot;button&quot; value=&quot;Submit&quot; onclick=&quot;check();&quot;>
</form>
</body>
</html>

test.java
Code:
import java.applet.Applet;
import java.awt.*;

public class test extends Applet{
	
	String s1 = null;
	
	public void init(){
		s1 = getParameter(&quot;str&quot;);
	}
	
    public void paint(Graphics g){    	
    	g.drawString(&quot;Value of parameter = &quot; + s1,10,10);
    }
}



Patrick
 
no, couldnt get that to work. Have u got it working?
however I have solved it in another way: using one javascript in each frame and with the help of mayscript, like:

some of the Form_frame:
function sendIt()
{
var theText=document.theForm.theName.value;
parent.applet_frame.aScriptFunction(theText);
}
<form action=&quot;javascript:sendIt();&quot; method=&quot;get&quot; name=&quot;Login&quot;>
<input type=&quot;text&quot; value=&quot;&quot; name=&quot;theName&quot; align=&quot;center&quot; maxlength=&quot;25&quot; size=&quot;40&quot;>
</form>


and then some frome the Applet_frame:
function aScriptFunction(aText)
{
document.theApplet.aFunction(aText);

}
<applet code=&quot;theApplet.class&quot; name=&quot;theApplet&quot; mayscript=&quot;mayscript&quot; width=&quot;250&quot; height=&quot;40&quot;>
</applet>

and the Applet:
public void aFunction(String someText)
{
System.out.println(&quot;Applet: got text:&quot;+someText);
}

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top