0

I'm currently coding a website on which I need to have a picture file and a txt file displayed on a HTML page that will vary by the date. I've got the picture set up, but I need to be able to show the contents of a txt file. I have it set up so that it will target a folder that is the number of the current month, then it will target a file that is the date. i.e. The variable image = "calendar/2/28.jpg" Here's the code, thanks:

http://pastebin.com/2LCT1qiG

*Note I'd rather not use anything besides javascript, HTML and CSS, but if jQuery or another language is necessary I will use that.

  • possible duplicate of [How do I load the contents of a text file into a javascript variable?](http://stackoverflow.com/questions/196498/how-do-i-load-the-contents-of-a-text-file-into-a-javascript-variable) – CyberDude Feb 28 '15 at 15:47
  • see this: http://stackoverflow.com/questions/196498/how-do-i-load-the-contents-of-a-text-file-into-a-javascript-variable – CyberDude Feb 28 '15 at 15:47

1 Answers1

0

You can use ajax to fetch text file:

without jQuery it will be:

var request = new XMLHttpRequest();
request.onreadystatechange = function () {
    var DONE = this.DONE || 4;
    if (this.readyState === DONE){
       document.getElementById("dailyText").innerHTML = request.responseText;
    }
};
request.open('GET', 'calendar/'+today+'.txt', true);
request.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
request.send(null); 

if you use jQuery the code will be much shorter:

$.get('calendar/'+today+'.txt', function(text) {
   $('#dailyText').html(text);
});
jcubic
  • 61,973
  • 54
  • 229
  • 402