-1

I have a file on my web server (test.txt) and I need to get the contents of it, then assign it to a variable for further manipulation and eventual output to the page, and this should happen every second or so (hence why PHP isn't really an option).

I've tried using jQuery get, XMLHttpRequest and Ajax - all without luck. If somebody could advise me on how I need to go about solving my issue, I would greatly appreciate it.

Ryan
  • 957
  • 5
  • 16
  • 34

1 Answers1

1

You can load your file content in a var this way -

var txt ="";

$.get('test.txt', function (data) {
    txt = data;
});

For loading every second or so,

setInterval(function(){
   $.get('test.txt', function (data) {
      txt = data;
   });
},1000)
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • I attempted your solution with the following piece of sample code `var txt =""; $.get('test.txt', function (data) { txt = data; }); alert(txt);` however, the alert box just comes up blank. – Ryan Apr 20 '13 at 18:07
  • You should put your alert inside of the function `$.get('test.txt', function (data) { txt = data; alert(txt); });` – Adil Shaikh Apr 20 '13 at 18:15
  • I did, but then the alert box didn't show at all. Is there some method to verify the file can be found etc before showing the alert box? – Ryan Apr 20 '13 at 18:29