Hi there RakiZtahX,
Welcome to the forums 
It would be better for you if your AJAX request was returning JSON, as opposed to a lump of text.
Maybe this is something for you to think about.
Anyway, as it is you need to do the following:- Fetch the contents of the text file using AJAX and store the response in a variable called 'data'
- The way to distinguish between "records" is that they are separated by a newline, therefore if you do: var items = data.split("\n");, you then have an array of records.
- Then, loop through the array of records and split each record at the pipe character: els = items[i].split("|");. This gives you a further array of the record's "attributes".
- Then select the first element in your new array, which is the name you are after, append it to a DocumentFragment
- Repeat this for all of your "records" and stick the fragment back into the DOM.
Here's an example:
HTML Code:
<!DOCTYPE HTML>
<html>
<head>
<!--http://www.sitepoint.com/forums/showthread.php?971960-Plz-help-i-want-to-limit-it-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Format AJAX response text</title>
</head>
<body>
<div id="header2">
<button onclick="syncText()">Pak</button>
<ul id="list"></ul>
</div>
<script>
function syncText() {
var xhr = new XMLHttpRequest();
xhr.open("get", "contacts.txt", false);
xhr.send(null);
if (xhr.status == 200) {
var data = xhr.responseText;
var items = data.split("\n");
var frag = document.createDocumentFragment();
for (i=0; i<items.length; i++){
els = items[i].split("|");
li = document.createElement("li");
li.innerHTML = els[0];
frag.appendChild(li);
}
document.getElementById("list").appendChild(frag);
} else {
alert("data retrieval failed...");
}
}
</script>
</body>
</html>
I hope this helps you.
Bookmarks