Live Feed From Local Text Document?

Gigabyte / Nvdaacpi
May 4, 2009 at 02:34:39
Specs: Microsoft Windows XP Professional, 2.31 GHz / 3070 MB
Hey,
I am writing a web-page, and i need some script (That is cross-browser compatible) that will read like a "Live Feed" from a text document, As this document may change at any time, and it needs to update the text ASAP.

The page its self cannot refresh on a continuous basis as that would just be annoying, so i was thinking that a JS box that refreshed by itself would be a possibility?

Anyways, my knowledge of anything beyond batch is so minuscule it may as well be non-existent, so any help is appreciated.


See More: Live Feed From Local Text Document?

Report •


#1
May 4, 2009 at 12:20:16
If you need to access a data file, then you may be able to get away with using perl, php, asp, etc along with an ajax routine that would post a request to read the file at a regular interval.

Does your host offer php?

______________________
My work in progress. I hate JS. :P


Report •

#2
May 4, 2009 at 15:38:13
I am my host - so yes, definitely.
If not, ill find a way to do it.
The problem is, i know hardly anything
about any of those languages.

Report •

#3
May 4, 2009 at 22:40:44
Could you or somebody else please explain what an AJAX routine is?
Sorry, but web development isn't my forte.

Report •

Related Solutions

#4
May 4, 2009 at 23:55:59
Do you think this would work?

<head>
<script src="text/javascript" src="ajaxroutine.js">
</script>
</head>

<body>

<script type="text/javascript">

function processGetPost(){
var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype
if (myajax.readyState == 4){
if (myajax.status==200 || window.location.href.indexOf("http")==-1){
if (myfiletype=="LEO")
alert(myajax.responseText)
else
alert(myajax.responseXML)
}
}
}

Ajax request.
ajaxpack.getAjaxRequest("CONNECTION.LEO", "", processGetPost, "txt")
</script>


And then further down the page in a box by its self:

ajaxpack.getAjaxRequest(ajaxpack.basedomain+"//home/homepage/CONNECTION.LEO", "", processGetPost, "txt")


Report •

#5
May 5, 2009 at 00:01:18
I'm not sure if it's what you're entirely after, but I thought you could possibly use two files - one would be the document that user's see and the other being a php file that loads the text document. In my brief test, I placed all three in the same directory - the html, the php, and the text data files.

The php file; I named it fileio.php

<?php

   $_POST["cmd"] = "update";

   if(isset($_POST["cmd"]) && $_POST["cmd"] === "update") {
   
      $fh = @fopen("data.txt", "r");
      $buf = "";
   
      if($fh && flock($fh, LOCK_EX)) {
         while(!feof($fh)) {
            $buf .= nl2br(fgets($fh));
         }

         flock($fh, LOCK_UN); 
         fclose($fh);
         die($buf);
      } else {
         die("file access error");
      }
   }
?>

The html file; I named it news.html

<html>
<head>

<script language="javascript">

function ajaxReq() {   
 
   ajax.open("POST", "fileio.php", true);
   ajax.onreadystatechange = ajaxRes;

   var params = "cmd=update";

   ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
   ajax.setRequestHeader("Content-length", params.length);
   ajax.setRequestHeader("Connection", "close");

   ajax.send(params);
}

function ajaxRes() {

   if (ajax.readyState == 4 && ajax.status == 200) {
      try {
         var res = ajax.responseText;  
         if(res != "file access error") {
            document.getElementById("news").innerHTML = res;
         } else {
            document.getElementById("news").innerHTML = "news temporarily unavailable"; 
         }           
      }  catch(e) { alert(e); }         
   }  
}

function getXHTTP() {

   var xhttp;

   try { 
      xhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
      try {
         xhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e2) {
           try {
              xhttp = new XMLHttpRequest();
           } catch (e3) {
              xhttp = false;
           }
      }
   }

   return xhttp;
}

var ajax = getXHTTP();

</script>
</head>

<body onload="setInterval('ajaxReq()', 5000)">
<div id="news"></div>
</body>
</html>

You *probably* wouldn't want to send a request every 5 seconds (setInterval('ajaxReq()', 5000)) though, but the above is a very basic test.

HTH

______________________
My work in progress. I hate JS. :P


Report •

#6
May 5, 2009 at 00:12:33
I've never used the syntax ajaxpack.ajaxobj before, so I can't really comment on it. All I can suggest is to try it out and if it works for your need, then give it a whirl. :)

______________________
My work in progress. I hate JS. :P


Report •

#7
May 5, 2009 at 00:14:49
Thanks alot, That seems somewhat more realistic than my... well... whatever that is.

I will give it a shot, thanks for your help, Ill post back if it works or not,

Good luck with your game.


Report •

#8
May 5, 2009 at 00:41:50
Ok, I really HATE working around a subject i know virtually nothing about...
What you presented didn't seem to do anything. I'm pretty certain its an error on my behalf though because i totally SUCK!

I cant even get my web page to place my banner image in the center...

There are several possibilities;
[1] I have changed something wrong. I need it to detect the file CONNECTION.LEO in the "data" file.
[2] It isn't compatible with my browser??? I am using ubuntu (my xp seems to have died) firefox V3.0
[3] some other part of my page is screwing it up.

I'm just going to post the entire script. please enlighten me :P

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Atoru Home Page</title>
<meta name="author" content="Atoru" >
<meta name="description" content="Home" >
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1" >
<meta http-equiv="Content-Script-Type" content="text/javascript" >
<meta http-equiv="Content-Style-Type" content="text/css" >
<script language="javascript">

function ajaxReq() {

ajax.open("POST", "fileio.php", true);
ajax.onreadystatechange = ajaxRes;

var params = "cmd=update";

ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
ajax.setRequestHeader("Content-length", params.length);
ajax.setRequestHeader("Connection", "close");

ajax.send(params);
}

function ajaxRes() {

if (ajax.readyState == 4 && ajax.status == 200) {
try {
var res = ajax.responseText;
if(res != "file access error") {
document.getElementById("news").innerHTML = res;
} else {
document.getElementById("news").innerHTML = "news temporarily unavailable";
}
} catch(e) { alert(e); }
}
}

function getXHTTP() {

var xhttp;

try {
xhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e2) {
try {
xhttp = new XMLHttpRequest();
} catch (e3) {
xhttp = false;
}
}
}

return xhttp;
}

var ajax = getXHTTP();

</script>

</head>
<body alink="#c43f82" vlink="#8f2b85" link="#f12323" text="#ffffff" bgcolor="#001902">
<table border="0" align="center" cellspacing="0" cellpadding="2" width="400" bgcolor="#5e1919"">
<tr color="blue"">

<td>

<H4 align="center">Welcome To The Atoru Home Page</H4></p> </td>
</tr>
</table>

<body onload="setInterval('ajaxReq()', 5000)">
<div id="connection"></div>

</body>
</html>

If its possible, could you please move the *.php file to the "scripts" folder?

Thanks so much for your help.
Atoru.


Report •

#9
May 5, 2009 at 02:30:24
Is connection.leo a plain text file? where is it relative to the html file? For example, if you have your site as mydomain and the user page is named news.html

mydomain
mydomain/news.html

where is "connection.leo" located relative to mydomain?
where is the "scripts" folder relative to mydomain?

For what you've posted, the body tag is required once. What I'd do is keep your existing body tag, but modify it a bit <body alink="#c43f82" vlink="#8f2b85" link="#f12323"
text="#ffffff" bgcolor="#001902"
onload="setInterval('ajaxReq()', 5000)">
and then remove the other body tag... <body onload="setInterval('ajaxReq()', 5000)">

Where you've got <div id="connection"></div>, if you're using that for the feed, then in the function ajaxRes, change document.getElementById("news").innerHTML to document.getElementById("connection").innerHTML

Getting back to the file paths; if you've got

mydomain
mydomain/scripts/
mydomain/news.html

in the function ajaxReq, change
ajax.open("POST", "fileio.php", true);
to
ajax.open("POST", "scripts/fileio.php", true);

Getting access to connection.leo will depend on where it is located to relative to fileio.php. If it's in the same directory, then you shouldn't have to change anything in fileio.php; however, if it's located in the same directory as news.html, then change $fh = @fopen("connection.leo", "r") to $fh = @fopen("../connection.leo", "r")

It's a bit late for me now, but I *think* that's right.

HTH

______________________
My work in progress. I hate JS. :P


Report •

#10
May 5, 2009 at 22:55:25
Really sorry about the delay.
I will post all relative files in my directory.

//home/web/index.htm
//home/web/scripts/fileio.php
//home/web/images/atoru.jpg
//home/web/data/CONNECTION.LEO

The main reason for this delay is because my ubuntu instalation seems to have decided to die once again,
Every now and then it decides to tskill my applications. (Idk what else you would call that.)

Thanks so much for your help. Ill give the new script a shot right now. Ill post back weather it does what its supposed to or not :P

The file CONNECTION.LEO is a plain .txt document, I just want it index.htm to post its entire content to the page.

I probably should use 4 letter extensions but that's a habit.


Report •

#11
May 6, 2009 at 20:10:22
Hey,
Not completly sure how to use the personal message thingy.
I'm not sure if the reply went thorough.

I got yours, but the link didn't seem to be there...?
Could you please send it again or post it on this forum page?

Thanks a heap,
Atoru


Report •

#12
May 6, 2009 at 22:00:17
I got your message ok; check your messenger for a plain text link. If it isn't there, well curses... I'll post them here. :P

______________________
My work in progress. I hate JS. :P


Report •

#13
May 6, 2009 at 22:54:10
IT WORKED!!!
Thankyou so much for your help!
Your Awesome!!!
ありがとう ございます!!
ありがとう ございます!!
ありがとう ございます!!
ありがとう ございます!!

Your site is awesome btw, i wish i was that good :P


Report •

#14
May 6, 2009 at 22:57:29
No problem; I'm glad it helped you out. :) Oh, don't worry, it wouldn't take much effort at all to surpass my site. lol

______________________
My work in progress. I hate JS. :P


Report •

#15
May 6, 2009 at 23:06:11
I'm sure it would.
You should have seen my old site.
I am so bad at simple things such as HTML (I learnt the basics from W3schools. Maybe htmlgoodies would've been a better choice.) That i cant even understand half of the source of your homepage. (I hope you don't mind, i wanted to see what master-work looked like :P)
Special thanks to you for the CSS sheet to, i had no idea how to link one lol.
I'm much more at home with old batch than this :P

Report •


Ask Question