...

View Full Version : Read from a .txt file ..



SwAsH
05-27-2004, 02:37 PM
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:


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

requestcode
05-27-2004, 02:55 PM
Client Side javascript can not read files or write to them.

joh6nn
05-27-2004, 03:03 PM
http://www.codingforums.com/showthread.php?p=178912#post178912

SwAsH
05-27-2004, 03:13 PM
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:


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

Rex
05-27-2004, 03:25 PM
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-us/script56/html/jsobjfile.asp

- Rex

SwAsH
05-27-2004, 03:37 PM
I got it to work with the following 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.

shlagish
05-27-2004, 10:12 PM
I don't know ASP, but you probably would need an ASP if statement that tests if the .txt file is found.

glenngv
05-28-2004, 04:58 AM
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.


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

SwAsH
05-28-2004, 07:51 AM
You made my day, here's the final code, thank you all for your help, would never had done it wihout you..



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

Kor
05-28-2004, 08:17 AM
http://www.rgagnon.com/jsdetails/js-0034.html

glenngv
05-28-2004, 10:50 AM
You made my day, here's the final code, thank you all for your help, would never had done it wihout you..



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


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);
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum