PDA

View Full Version : Reading a text file using Javascript



sujata_ghosh
04-10-2008, 11:24 AM
Hi!

can any one tell me how to read a content of a text file using Javascript and store it into form text box on form load?

here is the total html and js script which i have made:
i have a text file on the same folder where this html page is on webserver, i want the js will read the file take the data and put it into at here, on loading of the form:

<INPUT type="text" size=5 name="inrrate" value="">




<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="Javascript">
function gotvalue(vt,rt)
{
form.hide.value=rt;
return true;
}

function rates(form) {

if (form.hide.value=="inr")
{
b=eval(form.inrrate.value)
d=eval(form.inr.value)
new1=d/(b/10)
nn=Math.round(new1*Math.pow(10,3))/Math.pow(10,3)
form.usdcr.value=nn
}


if (form.hide.value=="usd")
{
b=eval(form.inrrate.value)
a=eval(form.usd.value)
new2=(a*b*0.1)
nn2=Math.round(new2*Math.pow(10,5))/Math.pow(10,5)
form.inrcr.value=nn2
}


}


</script>


</head>

<body onLoad="javascript:this.form.inrrate.disabled=true;">
<FORM name="form" method="post">
<table border="0" cellpadding="5" cellspacing="0" bgcolor="#ffffff">
<TR>
<TD width=510 colspan="3" class="tableup1" valign="top">
<b>Convert a USD amount given in Millions into a Rupee amount given in Crores or vice versa</b>
<br></TD>
</TR>
<TR valign="top">
<TD width=175 height="59" nowrap>Enter USD amount here:
<INPUT type="number" size=5 name="usd" value="" onchange="javascript:gotvalue(this.value,'usd');"></TD>
<TD width=119 nowrap><div align="center">USD-INR rate:
<INPUT type="text" size=5 name="inrrate" value="">
</div> </TD>
<TD width=152 nowrap>Rupee Amt in Crores:
<INPUT type="number" size=15 name="inrcr" value=""></TD>
</TR>
<TR valign="top">
<TD width=175 nowrap>Enter Rupee amount here:
<INPUT type="number" size=5 name="inr" value="" onchange="javascript:gotvalue(this.value,'inr');"></TD>
<td nowrap><div align="center"></div></td>
<TD width=152 nowrap>USD amount in Millions:
<INPUT type="number" size=15 name="usdcr" value="">
<INPUT type="hidden" size=15 name="hide" value="">
<br></TD></TR>
<TR>
<TD nowrap colspan="3">
<div align="left">
<INPUT type="button" value="Submit" name=action onclick="javascript:rates(this.form)"> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
<input type="reset" value="Reset" name=action onclick="javascript:clear(this.form)">
</div>
</TD></TR>
</TABLE>
</FORM>

</body>
</html>



any help will be highly appreciated.

Thannks,

codeexploiter
04-10-2008, 11:39 AM
Without using some activex functionality it is not possible to read and write files using Javascript. Assume that you've developed such a code that read/write the file using JavaScript it will work only in IE browser not on Mozilla based browsers like Firefox, flock.

You can view an example page in which they've explained how you can read / write files using JavaScript and activex based method

http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm

It is better if you use any server-side tool like PHP for accomplishing this task.