-
Dynamic Contact List
I have a company directory on my webiste. I'd like to be able to have a text file, each different contact name will be on a separate line, with the contact information of the person. I would like to dynamically create the company directory page by editing the directory text file and the page be created by what's inside. So, here are my questions:
1) How can I get a text file to be read from a webpage?
2) Has anyone ever done this and if so, got some code to share?
3) Am I going about this the wrong way? Thanks, Jeremy
-
As far as I know, ordinary textfiles aren't understood by HTML-pages or JavaScript.
I think you can better make it a js-file and link that within your page.
Create the contact list as an array of objects. I hope you understand how to create and manage objects within JavaScript. It's very useful once you can get grip on it.
Here's an example:
Code:
//I've made an add method to add stuff to an array easier
Array.prototype.add = Array_add;
function Array_add(item)
{
this[this.length] = item;
}
function Contact(par1, par2, par3)
{
this.par1 = par1;
this.par2 = par2;
this.par3 = par3;
return this;
}
var ContactList = new Array();
ContactList.add(new Contact("John", "Doe", 25));
ContactList.add(new Contact("Mary", "Pierson", 47));
ContactList.add(new Contact("Roger", "Johnson", 38));
In other JavaScript you can read / manipulate the contact list as if it was an ordinary array.
This will display the contact list in a table
Code:
document.write("<table border=\"1\">");
for(var i = 0; i < ContactList.length; i++)
{
document.write("<tr><td>" + ContactList[i].par1 + "</td><td>" +
ContactList[i].par2 + "</td><td>" + ContactList[i].par3 + "</td></tr>");
}
document.write("</table>");
I hope this will help.