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 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Reading External Text File

    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

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    163
    Thanks
    3
    Thanked 25 Times in 25 Posts
    try going through an iframe.
    for example: <iframe src="test.txt">

    x=document.getElementsByTagName('iframe')[0].contentWindow.document.body.innerHTML;
    will give you through original text inside a <pre> tag you can remove later. This works in firefox and IE, i don't know how to accomplish this in chrome

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    You should have just looked at this thread with msg posted about same time as yours:
    http://www.codingforums.com/showthread.php?t=200071

    If that doesn't get you well on your way, ask again.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Jul 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks alot, that seems to be a real good solution..
    I can remove PRE tag with some string manipulation easily..

    but I don't want that the text from the text file will be seen, is it possible to make the iframe invisible?
    if not, is there any other solution?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    You know, Gizmo, that's a great idea.

    You can even hide the <iframe>:

    <iframe style="display: none;">

    You can leave it visibile while you are developing the code and then hide it later.

    No need for <pre> tags. You don't really care what it *looks* like. The line breaks will be there, but won't be visible in the browser. So what.

    I don't see why you think this won't work in all browsers. Chrome handles <iframe> quite nicely. I use <iframe>s all the time--including even hidden iframes--and it all works with MSIE6/7/8, FF, Opera, Chrome, Safari.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    I wish now that I'd thought of this for my answer in the other thread. DOH on me!!!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Old Pedant View Post

    No need for <pre> tags. You don't really care what it *looks* like. The line breaks will be there, but won't be visible in the browser. So what.
    sure about that?
    run this IE8 developer tools test line:
    Code:
    document.body.innerHTML="1\n2\n3\n4"; alert(document.body.innerHTML)
    pasted firefox result:
    Code:
    1
    2
    3
    4
    sketched ie result:
    Code:
    1 2 3 4




    here is something that works the same in all browsers
    Code:
    function curl(U) {
        var X = !window.XMLHttpRequest ? 
          new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest ;
        X.open( 'GET', U, false);  X.send('');
      return X.responseText;
    }
    
    
    var myString = curl("test.txt");
    
    alert( myString  );
    ajax also has the following benefits over an iframe:
    • no history entry
    • no click in ie7
    • safely work on files other than .txt
    • no extra window object to waste CPU and RAM
    • no status bar activity
    • no waiting on the iframe to load before attempting to get string, can call anytime
    • invisible, even with CSS disabled
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • Users who have thanked rnd me for this post:

    Old Pedant (07-16-2010)

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    Well, that's actually exactly the code I used in my other answer.

    As demoed at http://www.MyWhizBang.com/simpleSearch.html

    So, maybe I did the right thing, after all. <grin/>

    Incidentally, this is *NOT* AJAX code. I guess it could be called "SJAX" ("Synchronous" as opposed to "Asynchronous") but since it's not XML just text, maybe it should be "SJAT"?? Hmmm...I think I like that: "Synchronous Javascript Access to Text".

    ********

    I do have to play Devil's Advocate, a little:

    Since what he wants is a set of *words*, he could presumably do
    Code:
    var text = [iframe contents].innerHTML;
    var words = test.split(/\s+/g); // split on *any* white space
    Clearly doesn't work if some of your "words" are actually multi word phrases.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,652
    Thanks
    80
    Thanked 4,640 Times in 4,602 Posts
    By the by, I *did* double check, and indeed MSIE converts the \n to a true space.

    I did txt.charCodeAt(1) and got 32.

    So thanks, RndMe, for keeping me from rewriting code only to find it won't work.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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