Javascript Dynamic Content

dominicb

New Member
Hi all

First time posting on this forum! I'm a bit of a javascript noob, but I'm trying.
By pinching bits off the internet and some serious amounts of trial and error, I cobbled together a small script and got it working.

The general idea is to have some dynamic content that changes every day. A cut down version of the code is attached, with just the first few facts, but I've also attached the script within an .html file that can run standalone.

Would it be a simple job to amend the code so that instead of reading through an array contained in the code, it reads the information from a text file instead?

Code:
<script type="text/javascript">

var eventList = [
["1/1","1660-Samual Pepys starts his famous diary.  In code.  Won't be decrypted for another 165 years."],
["2/1","1981-Murder charge brought against Yorkshire ripper.  Sentence will be extended to 'full life' in 2010."],
["3/1","1496-Entries in Leonardo da Vinci's notes suggested that he tested his flying machine today.  It didn't work."],
["4/1","1643-Force is equal to a nominated mass when multiplied by acceleration.  Happy Birthday Isaac Newton."],
["5/1","1531-Pope Clemens VII forbids Henry VIII to re-marry.  Will marry Anne Boleyn anyway in just over 2 years time."],
["6/1","1540-King Henry VIII marries 4th wife, Anne of Cleves.  Marriage will be annulled on 9 July this year."],
["7/1","1990-Tower Of Pisa closed to the public after leaning too far.  Won't reopen for nearly 12 years (15 December 2001)."],
["8/1","1642-Astronomer Galileo Galilei dies.  Church will apologise for its treatment of him 400 years later."],
["9/1","1799-Prime Minister William Pitt the Younger introduces income tax today as a TEMPORARY WARTIME MEASURE only."],
["10/1","1863-The first section of the London Underground was opened by Prime Minister Gladstone at Paddington."],
];

var now = new Date();
var today = now.getDate()+"/"+(now.getMonth()+1);
for (var i=0;i<eventList.length;i++) {
if (eventList[i][0] == today) {
document.write("<p>On this day in history : "+eventList[i][1]+"<\/p>");
}
}
</script>

Thanks for reading.

DominicB
 

Attachments

  • index1.zip
    19 KB · Views: 2
Last edited:

bcee

New Member
Look into AJAX, it can do this with JSON or XML. Also a JS framework like jquery will make this a breeze.
 
Top