Reading External Text File

Discussion in 'JavaScript' started by ranwsn, Jul 16, 2010.

  1. #1
    Hey guys,

    I'm writing a script and I've encountered a problem..
    I have a txt file with many words, each word in a different line.
    For example: the file words.txt contains:
    word1
    word2
    word3
    word4
    word5

    I need to load the content of the file into a variable in my script.
    I prefer that all the words will be in the same variable with line breaks, but if you'll figure out a way to put it in an array, it's ok too.
    I really don't know how to do it, and I tried to google but didn't understand.. By the way, I don't want to change the txt file to js file, I need it to remain txt..

    Can anybody help me with that?
    Thanks Alot,
    Ran
     
    ranwsn, Jul 16, 2010 IP
  2. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you could change the words.txt file to look like word1,word2,word3 using comma as a delimiter you could seperate the data and replace the commas with line breaks.
     
    Imozeb, Jul 16, 2010 IP
  3. ranwsn

    ranwsn Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hmmm I must leave the words in the txt file with line breaks, I've already thought about the word1,word2,word3 form but it's not goot for me.

    But my main problem is that I don't know how to read an external txt file..

    And by the way when I think of it doesn't matter if the data
    word1
    word2
    word3
    will be in an array or in a variable..

    Thanks, Ran
     
    ranwsn, Jul 16, 2010 IP
  4. peoplesmind

    peoplesmind Active Member

    Messages:
    107
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #4
    Javascript can't access file IO, so doing this is impossible--you should look into parsing this server-side (PHP/ASP/etc.), then handling the data on the client side (Javascript)

    Actually, thinking about it now--it is possible but I wouldn't recommend it. I believe there's an IE specific Java-extension in Javascript that would allow this, but it would only work in IE. You'd have to create java.io object and use that--but you wouldn't include anyone using IE or people who don't have Java enabled (different from JavaScript).
     
    Last edited: Jul 16, 2010
    peoplesmind, Jul 16, 2010 IP
  5. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, you would have to use a SSL (PHP ASP CF) to read the data from a text file. If you want the variable straight in javascript you can either use AJAX to get the data (.responseText) or you can get the variable through the SSL and populate a hidden text field with it to be read by javascript via getElementById('field'); I recommend using AJAX though because it is cleaner.
     
    Imozeb, Jul 16, 2010 IP
  6. ashishhbti

    ashishhbti Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    better to use xml file & use domxml parse to get the values..
     
    ashishhbti, Jul 21, 2010 IP
  7. schemo

    schemo Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    javascript CAN easily access file using small hidden signed java applet.
    http://www.youtube.com/watch?v=wqpwyAAhKGg
     
    schemo, Sep 27, 2010 IP
  8. jocurileus

    jocurileus Peon

    Messages:
    53
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I recommend transforming your .txt file in an xml format and then read it with javascript via xmldom!
     
    jocurileus, Sep 27, 2010 IP
  9. GFX_Lover

    GFX_Lover Peon

    Messages:
    60
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Convert your txtx file into a JSON format file.

    {"word1","word2","word3"}

    And use jQuery to get the data ;)
     
    GFX_Lover, Oct 3, 2010 IP
  10. krsix

    krsix Peon

    Messages:
    435
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Use \r\n or something as a delimiter instead of comma then.
     
    krsix, Oct 4, 2010 IP