Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts

    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.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts

  • #3
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts

    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.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,641
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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/);

  • #6
    Banned
    Join Date
    Oct 2005
    Location
    I'm in GMT -5
    Posts
    314
    Thanks
    0
    Thanked 1 Time in 1 Post
    .....
    Last edited by Ancora; 01-09-2006 at 09:41 PM.

  • #7
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts

    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.

  • #8
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You didn't say what the error message was.
    Try running it in Firefox to get better error description and location.

  • #10
    Regular Coder
    Join Date
    Oct 2003
    Location
    on a ship
    Posts
    574
    Thanks
    1
    Thanked 6 Times in 5 Posts
    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.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    You still didn't mention the error message.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •