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 11 of 11
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Read from a .txt file ..

    Hello,

    I have troubles reading contents from a .txt file within a JavaScript. I'm looking for a way to display contents in a JavaScript similair to fso.opentextfile in ASP.

    The actual code I'm using is:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function Show(filename,winTitle,description) 
    {
        var myImage = new Image();
        myImage.src=filename;
        properties='height=10,width=10';
        var imgWindow = window.open('','',properties);
    
        html = '<HTML>\n';
        html += '<HEAD>\n';
        html += '<TITLE>'+winTitle+'</TITLE>\n';
        html += '</HEAD>\n';
        html += '<BODY TOPMARGIN="0" MARGINHEIGHT="0" LEFTMARGIN="0" MARGINWIDTH="0">\n';
        html += '<IMG SRC="'+myImage.src+'" ONLOAD="resizeTo(document.bild.width+10,document.bild.height+100);" NAME="bild" ONCLICK="self.close();"><BR><BR>\n';
        html += ''+description+'\n';
        html += '</BODY>\n';
        html += '</HTML>\n';
        
        imgWindow.document.write(html);
    }
    </SCRIPT>
    I post to the script through: "javascript:Show('filename.jpg','05.jpg','05.txt')"

    Where 05.txt is the file that contains the text I want to display on the row with "+description+", the only thing I see there now with the code above is the actual filename.

    So the question is, how do I display the actual content of the file on that spot? The text file is one row only.

    Please advice ..
    Last edited by liorean; 05-27-2004 at 11:16 PM.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    626
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Client Side javascript can not read files or write to them.

  • #3
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #4
    New to the CF scene
    Join Date
    May 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Already tried..

    I've already tried those examples without getting it to work.

    The closest I've been so far is by mixing in some ASP code as follows:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function Show(filename,winTitle,description) 
    {
        var myImage = new Image();
        myImage.src=filename;
        properties='height=10,width=10';
        var imgWindow = window.open('','',properties);
    
        html = '<HTML>\n';
        html += '<HEAD>\n';
        html += '<TITLE>'+winTitle+'</TITLE>\n';
        html += '</HEAD>\n';
        html += '<BODY TOPMARGIN="0" MARGINHEIGHT="0" LEFTMARGIN="0" MARGINWIDTH="0">\n';
        html += '<IMG SRC="'+myImage.src+'" ONLOAD="resizeTo(document.bild.width+10,document.bild.height+100);" NAME="bild" ONCLICK="self.close();"><BR><BR>\n';
    	html += '<% set fso = Server.Createobject("Scripting.FileSystemObject") %>';
    	html += '<% path = "d:\wwwroot\001.txt"%>';
    	html += '<% set file = fso.opentextfile(path, 1) %> ';
    	html += '<% Response.write file.ReadLine%>';
        html += '</BODY>\n';
        html += '</HTML>\n';
        
        imgWindow.document.write(html);
    }
    </SCRIPT>
    The code above works, however the txt filename is hard coded. I've tried with: html += '<% path = "d:\wwwroot\"%>'+description+''; But i does not work..

    If anyone see a solution to my problem I would appreciate an code example.
    Last edited by liorean; 05-27-2004 at 11:17 PM.

  • #5
    Rex
    Rex is offline
    New Coder
    Join Date
    May 2004
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello SwAsH -

    You can't document.write() ASP code like that. ASP is executed by the server, when using JavaScript to write it, it just spits the stuff to the browser. But the server should parse it... So you can't do it that way.

    If you wish to read and write to files on the hard drive, however, that is possible. But you need an ActiveX object that only works in the Internet Explorer browser.

    Please visit this page: http://msdn.microsoft.com/library/en.../jsobjfile.asp

    - Rex
    Sincerely,
    Rex

  • #6
    New to the CF scene
    Join Date
    May 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Got it working ..

    I got it to work with the following code:

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function Show(filename,winTitle,description) 
    {
        var myImage = new Image();
        myImage.src=filename;
        properties='height=10,width=10';
        var imgWindow = window.open('','',properties);
    
        html = '<HTML>\n';
        html += '<HEAD>\n';
        html += '<TITLE>'+winTitle+'</TITLE>\n';
        html += '</HEAD>\n';
        html += '<BODY TOPMARGIN="0" MARGINHEIGHT="0" LEFTMARGIN="0" MARGINWIDTH="0">\n';
        html += '<IMG SRC="'+myImage.src+'" ONLOAD="resizeTo(document.bild.width+10,document.bild.height+100);" NAME="bild" ONCLICK="self.close();"><BR><BR>\n';
    objXml = new ActiveXObject("Microsoft.XMLHTTP");
    
    var datafile = description;
    
    objXml.open("GET", datafile, true);
    objXml.onreadystatechange=function() {
       if (objXml.readyState==4) {
       	   display(objXml.responseText);
      }
     }
    
    objXml.send(null);
    
    function display(msg) {
      imgWindow.document.write(msg);
    }
    
        html += '</BODY>\n';
        html += '</HTML>\n';
        
        imgWindow.document.write(html);
    }
    </SCRIPT>
    However one little problem persists, if there is no txt file available I get a "The Page Cannot Be Found" error. Is there any easy way besides having empty text files to not show any text when I do not want to.
    Last edited by liorean; 05-27-2004 at 11:18 PM.

  • #7
    Senior Coder
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    1,063
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I don't know ASP, but you probably would need an ASP if statement that tests if the .txt file is found.
    Shawn

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by SwAsH
    However one little problem persists, if there is no txt file available I get a "The Page Cannot Be Found" error. Is there any easy way besides having empty text files to not show any text when I do not want to.
    Code:
    objXml.onreadystatechange=function() {
       if (objXml.readyState==4) {
             switch(objXml.status){
                 case 200:
                    display(objXml.responseText);
                    break;
                 case 404:
                    alert("Page not found.");
                    break;
                 default:
                    alert("HTTP Error " + objXml.status);
                    break;
             }
         }
     }
    Last edited by glenngv; 05-28-2004 at 05:01 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #9
    New to the CF scene
    Join Date
    May 2004
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Thank you for all the help..

    You made my day, here's the final code, thank you all for your help, would never had done it wihout you..

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function Show(filename,winTitle,description) 
    {
        var myImage = new Image();
        myImage.src=filename;
        properties='height=10,width=10';
        var imgWindow = window.open('','',properties);
    
        html = '<HTML>\n';
        html += '<HEAD>\n';
        html += '<TITLE>'+winTitle+'</TITLE>\n';
        html += '</HEAD>\n';
        html += '<BODY TOPMARGIN="0" MARGINHEIGHT="0" LEFTMARGIN="0" MARGINWIDTH="0">\n';
        html += '<IMG SRC="'+myImage.src+'" ONLOAD="resizeTo(document.bild.width+10,document.bild.height+100);" NAME="bild" ONCLICK="self.close();"><BR><BR>\n';
    
    objXml = new ActiveXObject("Microsoft.XMLHTTP");
    var datafile = description;
    objXml.open("GET", datafile, true);
    objXml.onreadystatechange=function() {
       if (objXml.readyState==4) {
             switch(objXml.status){
                 case 200:
    			html += ''+(objXml.responseText)+'\n';
    			    html += '</BODY>\n';
    			    html += '</HTML>\n';
        			imgWindow.document.write(html);
                    break;
                 case 404:
    			    html += '</BODY>\n';
    			    html += '</HTML>\n';
        			imgWindow.document.write(html);
                    break;
                 default:
                    alert("HTTP Error " + objXml.status);
                    break;
             }
         }
     }
    objXml.send(null);
    }
    </SCRIPT>

  • #10
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by SwAsH
    You made my day, here's the final code, thank you all for your help, would never had done it wihout you..

    Code:
    <SCRIPT LANGUAGE="JavaScript">
    function Show(filename,winTitle,description) 
    {
        var myImage = new Image();
        myImage.src=filename;
        properties='height=10,width=10';
        var imgWindow = window.open('','',properties);
    
        html = '<HTML>\n';
        html += '<HEAD>\n';
        html += '<TITLE>'+winTitle+'</TITLE>\n';
        html += '</HEAD>\n';
        html += '<BODY TOPMARGIN="0" MARGINHEIGHT="0" LEFTMARGIN="0" MARGINWIDTH="0">\n';
        html += '<IMG SRC="'+myImage.src+'" ONLOAD="resizeTo(document.bild.width+10,document.bild.height+100);" NAME="bild" ONCLICK="self.close();"><BR><BR>\n';
    
    objXml = new ActiveXObject("Microsoft.XMLHTTP");
    var datafile = description;
    objXml.open("GET", datafile, true);
    objXml.onreadystatechange=function() {
       if (objXml.readyState==4) {
             switch(objXml.status){
                 case 200:
                    html += objXml.responseText+'\n';
                    break;
                 case 404:
    			    html += '</BODY>\n';
    			    html += '</HTML>\n';
        			imgWindow.document.write(html);
                    break;
                 default:
                    html += 'HTTP Error ' + objXml.status);
                    break;
             }
             html += '</BODY>\n';
             html += '</HTML>\n';
             imgWindow.document.write(html);
         }
     }
    objXml.send(null);
    }
    </SCRIPT>
    If you don't want to display something if any HTTP error occurs, then you can simplify it by just using if-condition instead of switch.
    Code:
    objXml.onreadystatechange=function() {
       if (objXml.readyState==4 && objXml.status==200){
           html += objXml.responseText+'\n';
       }
        html += '</BODY>\n';
        html += '</HTML>\n';
        imgWindow.document.write(html);
     }
    objXml.send(null);
    }
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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