PDA

View Full Version : Javascript-applet communication



rajyalakshmi
10-31-2006, 07:51 AM
Hi,
i've written an applet which reads text from text file stored on my desktop was_status.txt

A.java

import java.applet.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
public class A extends Applet
{
private TextArea textArea = new TextArea(100, 800);
String line="";

public void init( )
{
String strPath ="C:/Documents and Settings/rajyalakshmi/Desktop/WAS_STATUS.txt";
try{
BufferedReader objReader = new BufferedReader(new FileReader(strPath));
line = objReader.readLine();
}catch( IOException e)
{
e.printStackTrace();
}
System.out.println("val :"+line);
}
public boolean returnFileValue()
{
if("ON".equals(line))
{
return true;
}
else
{
return false;
}


}

}

o/p is : ON as the text file contains text: ON

Javascript file has to read the ON value from applet
The code test.htm

<HEAD>
<TITLE> New Document </TITLE>
<SCRIPT LANGUAGE="JavaScript">
function sampleTest()
{

var s=document.TestApplet.returnFileValue().value ;
alert(s);
}
</SCRIPT>

</HEAD>

<BODY onLoad="sampleTest()">
<APPLET CODE="A.class" NAME="TestApplet" WIDTH = "200" HEIGHT = "200">
</APPLET >
</BODY>
</HTML>

when i execute this in browser it displays blank page instead of display "ON"

Kindly ,can anyone suggest me on this

Thanks & Regards,
Lakshmi

Twey
10-31-2006, 10:03 AM
var s=document.TestApplet.returnFileValue().value ;
alert(s);Try:
window.alert(document.getElementsByName("TestApplet")[0].returnFileValue());Your markup needs some work, though. Run it through a validator (http://validator.w3.org/).