Read a file into Javascript variable from the server

J

joe

I need to read a file into a Javascript variable everytime a Javascipt function
is executed. The file is generated by a perl scipt. The filr (a text file) is in
the save directory on the server as the javascipt js and perl script.

I googled around and find a lot of info regarding some platform and browser. What
would be the most compatible way to do this?

The program is a simple order form that should display a "units left" value when
the javascipt is executed.
 
T

Thomas 'PointedEars' Lahn

joe said:
My code does something like this:
SCRIPT LANGUAGE="JavaScript">
http://validator.w3.org/

...
function showstatus()
{
var jmystat="";

eval('http://www.server.com/cgi-bin/script.pl');
http://jibbering.com/faq/#FAQ4_40
http://jibbering.com/faq/#FAQ4_43

/* at this point the script.pl has read a file from the server in to perl
variable called $mystat. I should now to get that into jmystat somehow */

You need to make an HTTP request. You can use e.g. (i)frames or XHR for that.


PointedEars
 
J

joe

Thomas 'PointedEars' Lahn said:
You need to make an HTTP request. You can use e.g. (i)frames or XHR for that.


PointedEars

Thanks! The HTTP request approach is what I want. I got it to work but with a
some problems. I downloaded some sample from the net and tested it. The problems
I am having is:

- loadXMLDoc('test.dat') does not work but loadXMLDoc('test.bin') works. They are
the same file the latter having a different name.

- loadXMLDoc('/somedir/test.bin') does not work

- loadXMLDoc('//somedir//test.bin') does not work

(somedir is an existing directory)
I've made full access to the file. Are there some rules as to what names I can
use and where they can reside?



Here's the full source

<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7, Firefox, Opera, etc.
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlhttp!=null)
{
xmlhttp.onreadystatechange=state_Change;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
else
{
alert("Your browser does not support XMLHTTP.");
}
}

function state_Change()
{
if (xmlhttp.readyState==4)
{// 4 = "loaded"
if (xmlhttp.status==200)
{// 200 = "OK"
alert("Success:" + xmlhttp.responseText);
}
else
{
alert("Problem retrieving XML data:" + xmlhttp.statusText);
}
}
}
</script>
</head>

<body>
<h2>Using the HttpRequest Object</h2>

<button onclick="loadXMLDoc('test.dat')">Get XML</button>

</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top