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 13 of 13
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Mumbai, India
    Posts
    218
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Internet Explorer >> 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

    http://www.dynamicdrive.com/dynamicindex2/xmlticker.htm

    It works only on browsers supporting XML (IE)

    Enjoy!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    go to http://www.devguru.com for some tutorials on XML

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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!

  • #4
    New to the CF scene
    Join Date
    Jul 2005
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote 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

    http://www.dynamicdrive.com/dynamicindex2/xmlticker.htm

    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 ---<

  • #6
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #7
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ---<

  • #8
    Senior Coder
    Join Date
    Apr 2005
    Posts
    1,051
    Thanks
    0
    Thanked 0 Times in 0 Posts
    one example:
    http://rlemon.com/rltp/

    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 ---<

  • #9
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,493
    Thanks
    3
    Thanked 499 Times in 486 Posts
    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/

  • #10
    New to the CF scene
    Join Date
    May 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,273
    Thanks
    10
    Thanked 581 Times in 562 Posts
    Quote 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);
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #12
    New to the CF scene
    Join Date
    May 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #13
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,892
    Thanks
    202
    Thanked 2,528 Times in 2,506 Posts
    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.


  •  

    Posting Permissions

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