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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read the contents of a file on the web into a variable

Status
Not open for further replies.

Manderby

Programmer
Sep 14, 2004
5
CH
I'm feeling a little confused. After searching for two hours I haven't even been able to answer the question if it is possible or not:

In Javascript, I want to open an url and store the contents in a variable.

If I would write this in PHP, I would use fopen or file_get_contents. But I need to have this in Javascript. I don't want to use Java nor PHP nor ASP nor something else. And I don't want the page to be reloaded. Nothing with frames (not even iframes) or any other tricks. It should be plain Javascript. Nothing with ActiveX or any other platform-dependant stuff. This should be possible, or not?

What I have:
I've written a sendmail.php file, which sends as many mails as possible to different people during the period of one second. After this second, the script returns one single number: the number of mails sent. For precision: this number isn't a return-value of a function but the html-output. So if you would access this script in a normal browser with the address " you would see one single number on your screen.

What I want to do:
I want to write a html-page with a Javascript-Statusbar which shows me, how many mails I've sent till now (10% sent, 20% sent, ...). With the Javascript-function I would periodically call the sendmail.php-file to do its job and getting back the number of mails sent.

So. I need something like

num=getContents("
But... Well I haven't figured out, how to do this. And that's where I hope to get tons of information in this forum. :)

Thanks in advance.
 

This cannot be done client-side with plain old JavaScript (no ActiveX, Java applets, etc etc etc).

In IE you could use the FileSystemObject ActiveX control to do this, but as you said, you don't want to use ActiveX.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Thanks,

So I have to find a dirty solution.

well, at least I know now, that it isn't possible. Strange. For me, this would be one of the first functions to implement for a language like Javascript. Pretty essential for good coding. Or at the other hand, probably my coding isn't that good. :)

Thanks again and greetings from Switzerland.
 
Manderby,

Dan is right on with his assesment, you can't "dump a page into a variable".

But some dialog may prove fruitful here.

You can get php to communicate with javascript.
(set a variable and it's value)

Like:

upDater.html
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>PHP Status Sender</title>
<body>
<script src="upDater.php"></script>

</body>
</html>

upDater.php
Code:
<?php

// the value below could come from a file
$phpvar = 4;

$js = <<<END
var jsvar = $phpvar;
alert("This value came from a php variable " + jsvar);
END;

print $js;

?>


Hope it's helpful.

Good Place to "Learn More">>
 
Thank you Lrnmore,

a very nice trick. I could use this snipplet to create a page updating itself via php. I have to think about it but I see one problem, which was the same problem I had before and which was the base-problem why I started to think of my problem described above (didn't mentioned it beacause I know there is no solution): The max-execution-time of php-scripts. As I normally run scripts on servers running php in safe-mode (and that's good), I have no chance to reset this timer and wanted to avoid this execution-time by calling multiple php-scripts on the client-side.

I could avoid the problem using header(). But as I need to give the user a feedback about how much time/mails have passed periodically, I cannot use this function either.

I think, the best thing to do is to create a php-script with an automatic javascript-redirect after a small number of milliseconds at the end of the HTML-code (when I can be sure that the script has been executed completely). The page reloads with an updated number of mails passed, displays the result and waits for the redirect to occur. HTML-redirect is dirty and incontrollable.

The only thing that disturbes me is the reload. How does different browsers display the results? Will they wait till the new page is loaded or will they show a blank screen during the reload... No answer needed. I will find out myself. Trying to give the browser as less information as possible at the latest possible time, so it can wait with redisplay.
 
You should have no problem doing this with PHP, but I thought you didn't want to use PHP?

I don't want to use Java nor PHP nor ASP nor something else

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
@BilliRay:

That's right. But as I've seen now, that it isn't possible with plain Javascript, I probably should find another solution with reloading pages which is easier to implement with php and Javascript together.

@arst:
I've already seen XMLHTTPRequest once and found that it isn't platform-independent. But maybe I should consider this as a second possibility. Gracias.
 
Manderby of Switzerland,

Here's an idea that might be useful for you.

You can write out a text file (generated from php).
It would look like:

Code:
js_sent = 15; //of course 15 represents the emails sent

We can set a timeout after checking the value.

When the php has completed it's task you could make the value
of js_sent = 'finished'( in the text file ).

No reloads here,
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
  <title></title>
<script>
var tm = null;
var js_sent = 0;

function ckPhp(){
var dispObj = document.getElementById('dO');
//the next line would be if(js_sent != 'finished'){ 
if(js_sent < 20){
   setTimeout('ckPhp()',1500);
   //append script using "create element"
   //script src would be (.txt) or (.php) file for value
   // next line mimics what would come from "appended script"
   js_sent += 5; 
   dispObj.innerHTML = js_sent;
   }
}
   
</script>  
</head>
<body onload="setTimeout('ckPhp()',1500)">
<span id="dO">0</span>
</body>
</html>

If you think this would work out, I'll have to review the "append" process.

Or someone else can pick up here..

Thanks,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top