Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-19-2005, 06:47 AM   PM User | #1
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
txt file line retrieval

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.....
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 12-19-2005, 07:11 AM   PM User | #2
_Aerospace_Eng_
Supreme Master coder!


 
_Aerospace_Eng_'s Avatar
 
Join Date: Dec 2004
Location: In a place far, far away...
Posts: 19,293
Thanks: 2
Thanked 1,044 Times in 1,020 Posts
_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light_Aerospace_Eng_ is a glorious beacon of light
maybe this will help http://www.dynamicdrive.com/dynamici...jaxcontent.htm
_Aerospace_Eng_ is offline   Reply With Quote
Old 01-08-2006, 06:37 AM   PM User | #3
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
works, but not quite what im looking for

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.
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 01-08-2006, 08:47 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,454
Thanks: 0
Thanked 498 Times in 490 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 01-09-2006, 02:25 AM   PM User | #5
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Quote:
Originally Posted by felgall
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.
Code:
var arr = page_request.responseText.split(/\n|\r\n/);
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 01-09-2006, 11:31 AM   PM User | #6
Ancora
Banned

 
Join Date: Oct 2005
Location: I'm in GMT -5
Posts: 314
Thanks: 0
Thanked 1 Time in 1 Post
Ancora is on a distinguished road
.....

Last edited by Ancora; 01-09-2006 at 09:41 PM..
Ancora is offline   Reply With Quote
Old 01-13-2006, 09:36 PM   PM User | #7
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
hrmmmmm

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?
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 01-13-2006, 11:10 PM   PM User | #8
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
this is what i have so far:


Code:
<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)
Code:
Brandon 23 texas
Lucy 20 florida
Bart 22 texas
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 01-17-2006, 02:29 AM   PM User | #9
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You didn't say what the error message was.
Try running it in Firefox to get better error description and location.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 01-18-2006, 11:37 PM   PM User | #10
brandonH
Regular Coder

 
Join Date: Oct 2003
Location: on a ship
Posts: 574
Thanks: 1
Thanked 6 Times in 5 Posts
brandonH is on a distinguished road
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):
Code:
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:
Code:
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.....
__________________
I make no attempt at pretending like I'm a professional. I offer help with what knowledge I do have.
brandonH is offline   Reply With Quote
Old 01-19-2006, 01:18 AM   PM User | #11
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You still didn't mention the error message.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:07 PM.


Advertisement
Log in to turn off these ads.