...

View Full Version : txt file line retrieval



brandonH
12-19-2005, 07:47 AM
I've never tried to do this before other than cookies.....

I want to retrieve text from a saved txt file on my website and insert the text into the web pages html.

not too sure if javascript can do this or not.....

_Aerospace_Eng_
12-19-2005, 08:11 AM
maybe this will help http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm

brandonH
01-08-2006, 07:37 AM
that works great, but it grabs the entire content of the file and loads it.

what im trying to do is grab the entire content of a txt file and pick the txt file apart line by line, then display parts of a certain line in a div.

example:

<txt file content>

Brandon 56 beastlord
Lucy 54 monk
Glinda 63 Cleric
ect...
ect...
ect....

</txt file content>
i want to grab say line 2 (Lucy 54 monk) then display just that, or preferably set it to a variable so that i can pick it apart even further into (Lucy) then (54) then (monk).

i can pick the line apart once it is set to a variable, but i dont know how to retrieve it from the txt (not cookie) and set the txt files contents as a variable.

felgall
01-08-2006, 09:47 PM
Load it into an array - one line per entry in the array - and then select the entry from the array that contains the required line.

glenngv
01-09-2006, 03:25 AM
Load it into an array - one line per entry in the array - and then select the entry from the array that contains the required line.
You can load it into an array by splitting the responseText with newline.

var arr = page_request.responseText.split(/\n|\r\n/);

Ancora
01-09-2006, 12:31 PM
.....

brandonH
01-13-2006, 10:36 PM
ok encountering problems with that suggestion.

var arr = page_request.responseText.split(/\n|\r\n/);

using .split() causes a script error. The thing is it works, but it still causes a script error. I even tried doing this:

var arr = page_request.responseText;

var arr2 = arr.split(/\n|\r\n/);

but that causes errors as well.

the only way i can get this to work without errors is to write the entire txt file into a <div> and then use split() on the innerHTML of the <div>. which is not what i want to do because once the txt file contents are written to the div the split() no longer recongnizes the original line returns in the txt file, it just sees a long string (one line) of text. I need to pull the individual lines apart.

for some reason using split() with responseText just causes errors, but not errors that keep it from working. I just dont want the window to say there are errors, when really there aren't.


any ideas on why it say there is an error when really there isn't?

brandonH
01-14-2006, 12:10 AM
this is what i have so far:




<html>
<head>

<script type="text/javascript">

function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
page_request.open('GET', url, true)
page_request.send(null)
}

function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
data=page_request.responseText.split(/\n|\r\n/);
document.getElementById(containerid).innerHTML+="<b>line 1 of txt file:</b> "+data[0]+"<br><b>line 3 of txt file:</b> "+data[2];
}

</script>
</head>
<body>
click on the link to load all the text inside the txt file.
<a href="javascript:ajaxpage('test.txt', 'contentarea');">test</a>
<div id="contentarea"></div>
</body>
</html>


the test.txt file contains the following:
(the script assumes that the test.txt file is in same directory as the windows html file)


Brandon 23 texas
Lucy 20 florida
Bart 22 texas

glenngv
01-17-2006, 03:29 AM
You didn't say what the error message was. :rolleyes:
Try running it in Firefox to get better error description and location.

brandonH
01-19-2006, 12:37 AM
i found what was causing the error...... i dont know why it was causing it though, al i know is when i changed it, i didnt get any errors.

part of coed chaged (old):


function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
data=page_request.responseText.split(/\n|\r\n/);
document.getElementById(containerid).innerHTML+="<b>line 1 of txt file:</b> "+data[0]+"<br><b>line 3 of txt file:</b> "+data[2];
}

rewritten:


function loadpage(page_request, containerid){
if (page_request.readyState == 4 ){
if(page_request.status==200 || window.location.href.indexOf("http")==-1){
data=page_request.responseText.split(/\n|\r\n/);
document.getElementById(containerid).innerHTML+="<b>line 1 of txt file:</b> "+data[0]+"<br><b>line 3 of txt file:</b> "+data[2];
}}
}



thanks for all the help guys.....

glenngv
01-19-2006, 02:18 AM
You still didn't mention the error message.:rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum