PDA

View Full Version : how to set a variable from an external source (server)?


james erskine
09-20-2002, 08:14 PM
I have a javascript, on the client side, that shows a progress bar. The progress bar corresponds to a task running on the server.

I would like to set the value of a variable, in the javascript, according to a value that resides in a file on the server.

Is there a way to open and read the contents of a file using javascript.

eg. openfile(filehandle, http://someaddress/file.txt)
x = filehandle;

Thanks,

James

Spookster
09-21-2002, 08:07 AM
Javascript cannot open and read text files. You would be better of trying to do this with a server-side language such as PHP.

joh6nn
09-21-2002, 09:15 AM
server side scripts can return files of pretty much any format; all you have to do is have the server side return the right header. so, you have a server side script that reads a file, and returns that info as the appropriate javascript variable, with a javascript header ( text/javascript, i believe ), and then include it like this:

<script src="path/to/serverside.php"></script>

piglet
09-21-2002, 09:56 AM
Hi James,

As joh6nn says, you can return a script to the client like this from any server side language.

Much more exciting and less well-known is that using DOM scripting, you can actually load scripts on-the-fly as required - and not only that...you can identify them and reload them to effectively 'Poll' the server - from client side Javascript!

This is the mechanism:

1. Identify the Head fof your document
2. Check for your polling script - if there, delete it from the document
3. Create a new DOM element for your script
4. Set the script attributes
5. Attatch the script to the header

As this uses the DOM it will work in IE (5+?) and N6+/Mozilla - NOT Netscape 4.

Here's a little example to demonstrate. This should give you the information you need for your 'Progress bar':

Here's the client side bit:

<head>
<META HTTP-EQUIV="Expires" CONTENT="0">
<title>My client</title>

<script type="text/javascript">
var myscript = 'time2.php?';
var timer;

function reloadScript() {
var head = document.getElementsByTagName('head').item(0);
var old = document.getElementById('scriptId');
if (old) head.removeChild(old);

script = document.createElement('script');
var now= new Date();
script.src = myscript+now.valueOf();
script.type = 'text/javascript';
script.defer = true;
script.id = 'scriptId';

void(head.appendChild(script));
}

function poll(t){timer=setInterval('reloadScript()', t);}

</script>
</head>

<body>
On the server: <span id="time">&nbsp;</span><BR><BR>
<a href="javascript: poll(2000)">Start Polling!</a>;
<a href="javascript:clearInterval(timer);">Stop polling</a>
</body>
</html>


(note that "java script" should read "javascript" in the <a>'s above)

Here's an example Server side bit - in this case PHP - but can be any language:

<?php
$today = getdate();
$weekday = $today['weekday'];
$hours = $today['hours'];
$minutes = $today['minutes'];
$seconds = $today['seconds'];
header ("Content-type: text/javascript");
echo "document.getElementById('time').innerHTML='It is $weekday at $hours:$minutes:$seconds'";
?>