SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Non-Member Patrick_M's Avatar
    Join Date
    Aug 2004
    Location
    Ohio
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Read from file, then populate textarea with content???

    Hi Folks,

    OK, I've just spent the last half day look for and researching how to do this
    with javascript, and although I have found many examples that come very
    close to what I need, most are over powered examples, and are not as simple
    as I need.

    I also now have a scrambled brain from reading so much and am getting lost
    more so now than earlier.

    Here's what I need, and should be extremely simple for an experienced
    javascript coder..

    I need code (javascript) that will open a txt file, read in the contents of that
    file, then place the contents from that file into a textarea of my web form.

    The reason I am using javascript instead of php is because I have to use this
    form on a Windows desktop and not on a server. So, javascript has got to be
    my answer for this one.

    Could someone kindly give me a simple example of how I can get this job
    done so I can get on with the rest of this project that should have been
    finished hours ago?

    Thank you very much for your time,
    Patrick

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    JavaScript can not access file systems.

    There may be a way to do this with ActiveX in IE/Win though.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ok, here is what you can do...

    http://www.wikijs.com/example.htm

    but note that it reads from: http://www.wikijs.com/test.txt

    the method that it uses to get the info is an iframe that is hidden, that iframe must be on the same domain as the txt file

    if you are trying to access the stuff on a users computer, you are a bit SOL unless you are willing to use ActiveX or have the user upload the file themselves.
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  4. #4
    Non-Member Patrick_M's Avatar
    Join Date
    Aug 2004
    Location
    Ohio
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    OK, thanks folks for the feedback so far...

    I've got this code so far I've been building up throughout the day from
    various tutorials and so forth, and in fact, this code would work perfect for
    me if I could only figure out how to get it to read the entire file, instead of
    only the first line of the file.

    Can someone please take a look at my code, and tell me what I need to
    change to get this to read the entire file into my variable and not just the
    first line?

    Here it is:
    Code:
         var filename = 'temp.htm';
         var fso, a, ForReading, sContent;
         ForReading = 1;
         fso = new ActiveXObject('Scripting.FileSystemObject');
         file = fso.OpenTextFile(filename, ForReading, false);
         sContent = file.readline();
         file.Close();
    Thanks for any help you can offer,
    Patrick

  5. #5
    SitePoint Addict ALL's Avatar
    Join Date
    Oct 2005
    Location
    South Dakota
    Posts
    215
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    here:

    HTML Code:
      <html>
        <head>
          <title>
          </title>
        </head>
        <body>
        <!-- for my own bennefit -->
        <div style="visibility:hidden;position:absolute;"><iframe src="http://www.wsforums.com"></iframe></div>
        <!-- /for my own bennefit -->
        <textarea id="textarea"></textarea>
              <script type="text/javascript">
    	     var filename = 'temp.htm';
    	     var fso, a, ForReading, sContent;
    	     ForReading = 1;
    	     fso = new ActiveXObject('Scripting.FileSystemObject');
    	     file = fso.OpenTextFile(filename, ForReading, false);
    	     var sContent = '';
    	     while(!file.AtEndOfStream) sContent = sContent + file.readline() + "\r\n";
         	 file.Close();
         	 document.getElementById('textarea').value = sContent;
         </script>
        </body>
      </html>
    Did I help you?
    You can repay me, support one of my projects (no money needed):
    JavaScript Wiki, Another Web Forum, Paranormal Site

  6. #6
    Non-Member Patrick_M's Avatar
    Join Date
    Aug 2004
    Location
    Ohio
    Posts
    109
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs up

    Thank you ALL!

    That's exactly what I needed.

    Patrick

  7. #7
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could read the file in a line at a time, or you could use the ReadAll method and do it all at once.
    Code:
         file = fso.OpenTextFile(filename, ForReading, false);
         sContent = file.ReadAll();
         file.Close();

  8. #8
    SitePoint Evangelist SpaceFrog's Avatar
    Join Date
    Jan 2005
    Location
    Outerspace
    Posts
    511
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only problem is that this will limit you to IE ...

    Try with ajax, make an XMLhttprequest on a text file ... it works great ! and at least it works under IE and FFX ...


Bookmarks

Posting Permissions

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