Web Analytics Made Easy -
StatCounter >> Reading text file using JavaScript - CodingForum

Announcement

Collapse
No announcement yet.

>> Reading text file using JavaScript

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • >> Reading text file using JavaScript

    Somebody had posted this query on how to read text files using JavaScript.......can't find that post now...

    Anyways, though it's not possible to read text-files using JavaScript it is possible to read XML files using it.

    Here is an XML based JavaScript Ticker

    This is a XML based JavaScript Ticker that can tick any number of messages.


    It works only on browsers supporting XML (IE)

    Enjoy!
    Scripting | JavaScripts | PerlScripts | Python Scripts | Articles | My Journal

  • #2
    go to http://www.devguru.com for some tutorials on XML
    Glenn
    vBulletin Mods That Rock!

    Comment


    • #3
      You can use behaviors. Here's an example that I've posted here "for your viewing pleasure" :

      Code:
      <html xmlns:behave_yaself>
      <head>
      <title>Behaviors</title>
      <script type="text/javascript">
      function downloadEnd(e) {
      	document.all.download.value=e;
      }
      </script>
      <style type="text/css">
      textarea {
      	background-color: #000;
      	color: #FFF;
      	scrollbar-face-color: #FFF;
      }
      </style>
      </head>
      <body>
      <behave_yaself:download id="currFile" style="behavior:url(#default#download);" />
      Click <a href="javascript:document.getElementById('currFile').startDownload(location.href,downloadEnd)">here</a>
      to start the download. 
      <br />When the download is finished, the text will be displayed in the <b>TEXTAREA</b> below.
      <p><textarea rows="20" cols="40" id="download"></textarea></p>
      </body>
      </html>
      Hope that helps!

      Happy coding!

      Comment


      • #4
        If the above javascript is used in a asp.net web form, it will show the contents of the aspx file. But can you tell me if the above javascript can be modified to read any text file in the client machine? I tried changing it to :
        javascript:document.getElementById('currFile').startDownload('D:\ReadFile.txt',downloadEnd)"
        But it is throwing "permission denied" javascript error. Any workaround?

        Comment


        • #5
          Originally posted by premshree
          Somebody had posted this query on how to read text files using JavaScript.......can't find that post now...

          Anyways, though it's not possible to read text-files using JavaScript it is possible to read XML files using it.

          Here is an XML based JavaScript Ticker

          This is a XML based JavaScript Ticker that can tick any number of messages.


          It works only on browsers supporting XML (IE)

          Enjoy!
          Sorry but that last note about reading text files is not possible in JS is incorrect.

          It is possible, however the user has to accept an activeX component.

          you can parse the users FSO via js with almost the exact same functions you use in ASP

          Look up the JS Drive object and File objects
          public string ConjunctionJunction(string words, string phrases, string clauses)
          {
          return (String)(words + phrases + clauses);
          }
          <--- Was I Helpfull? Let me know ---<

          Comment


          • #6
            No HTTP served page should access your local harddrive unless you allow that to happen, and as mentioned that would require allowing either the Scripting FileSystemObject within MS IE, or by setting browser properties to allow such a transaction to happen via XMLHttpRequest objects.

            Comment


            • #7
              which can be done with signed scripts (RE: Allowing the FSO access)
              public string ConjunctionJunction(string words, string phrases, string clauses)
              {
              return (String)(words + phrases + clauses);
              }
              <--- Was I Helpfull? Let me know ---<

              Comment


              • #8
                one example:
                Own this domain today. We make your shopping experience easy. Friendly and quick customer service.


                note, there are limitations to this code (but can be worked out) - also, there is very little documentation on the JS syntax.
                public string ConjunctionJunction(string words, string phrases, string clauses)
                {
                return (String)(words + phrases + clauses);
                }
                <--- Was I Helpfull? Let me know ---<

                Comment


                • #9
                  Code:
                  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                      "http://www.w3.org/TR/html4/loose.dtd">
                  
                  <html>
                  
                  <head>
                    <title></title>
                  </head>
                  
                  <body>
                  <script language="JavaScript" type="text/javascript">
                  <!--
                  var srcFrame;
                  //External content into a layer
                  function loadOuter(doc) {
                   srcFrame = document.getElementById("hiddenContent");
                   srcFrame.src = doc;
                   // workaround for missing onLoad event in IFRAME for NN6
                   if (!srcFrame.onload) {
                    setTimeout("transferHTML()", 1000)
                   }
                  }
                  
                  function transferHTML(){
                   srcContent='';
                   if (srcFrame.contentDocument){
                    srcContent=srcFrame.contentDocument.getElementsByTagName("BODY")[0].innerHTML;
                   }
                   else if (srcFrame.contentWindow){
                    srcContent=srcFrame.contentWindow.document.body.innerHTML;
                   }
                   document.getElementById("outerDisplay").innerHTML = srcContent
                  }
                  
                  
                  var DocAry=new Array('Test.txt','Test2.txt');
                  
                  function SelectList(v){
                   if (v>0){
                    loadOuter(DocAry[v-1]);
                   }
                  }
                  
                  //-->
                  </script>
                  
                  <INPUT TYPE="button" VALUE="Test.txt" onClick="loadOuter('Test.txt')" >
                  
                  <INPUT TYPE="button" VALUE="Test2.txt" onClick="loadOuter('Test2.txt')" >
                  <br>
                  
                  <select name="fred" size="1" onchange="SelectList(this.selectedIndex);">
                  <option >Select a List</option>
                  <option >List 1</option>
                  <option >List 2</option>
                  </select>
                  
                  <div id="outerDisplay"></div>
                  
                  <iframe  id="hiddenContent" width="200" height="200" style="position:absolute;visibility:hidden;" ></iframe>
                  
                  </body>
                  
                  </html>
                  Code:
                  Test.txt
                  Item 1
                  Item 2
                  Item 3
                  Item 4
                  Item 5
                  Item 6
                  Item 7
                  Code:
                  Test2.txt
                  Item 11
                  Item 12
                  Item 13
                  Item 14
                  Item 15
                  Item 16
                  Item 17
                  Vic

                  God Loves You and will never love you less.

                  http://www.vicsjavascripts.org/Home.htm

                  If my post has been useful please donate to http://www.operationsmile.org.uk/

                  Comment


                  • #10
                    Reading and writing to iframe?

                    That's a great solution! is it possible to access the text in the iframe? I mean, can I transfer each line to a variable?

                    Comment


                    • #11
                      Originally posted by kirtley View Post
                      That's a great solution! is it possible to access the text in the iframe? I mean, can I transfer each line to a variable?
                      Your'e new, but it's normally bad form to hop on old posts.

                      I would (shamelessly) recommend you check out this more recent post about loading and saving files with javascript.

                      using it, if you wanted and array of the lines in myfile.txt, you could say:
                      Code:
                      var lines=IO("myFile.txt").split(/\r?\n/g);
                      Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

                      Comment


                      • #12
                        hi, can you please give me a better solution for

                        if (!srcFrame.onload) {
                        setTimeout("transferHTML()", 10);
                        }
                        else{
                        setTimeout("transferHTML()", 10);
                        }

                        because, it works for IE and FF but not for Google chrome. Thank you

                        Comment


                        • #13
                          adarshyam - Please do not hijack someone else's (old) thread. Start a new thread of your own, paying attention to the forum Rules and Guidelines before you post.

                          All the code given in this post has been tested and is intended to address the question asked.
                          Unless stated otherwise it is not just a demonstration.

                          Comment

                          Working...
                          X