...

View Full Version : Reading External Text File



ranwsn
07-16-2010, 06:38 PM
Hey guys,

I'm writing a script and I've encountered a problem..
I have a txt file with many words, each word in a different line.
For example: the file words.txt contains:
word1
word2
word3
word4
word5

I need to load the content of the file into a variable in my script.
I prefer that all the words will be in the same variable with line breaks, but if you'll figure out a way to put it in an array, it's ok too.
I really don't know how to do it, and I tried to google but didn't understand.. By the way, I don't want to change the txt file to js file, I need it to remain txt..

Can anybody help me with that?
Thanks Alot,
Ran

gizmo1650
07-16-2010, 07:39 PM
try going through an iframe.
for example: <iframe src="test.txt">

x=document.getElementsByTagName('iframe')[0].contentWindow.document.body.innerHTML;
will give you through original text inside a <pre> tag you can remove later. This works in firefox and IE, i don't know how to accomplish this in chrome

Old Pedant
07-16-2010, 08:11 PM
You should have just looked at this thread with msg posted about same time as yours:
http://www.codingforums.com/showthread.php?t=200071

If that doesn't get you well on your way, ask again.

ranwsn
07-16-2010, 08:11 PM
Thanks alot, that seems to be a real good solution..
I can remove PRE tag with some string manipulation easily..

but I don't want that the text from the text file will be seen, is it possible to make the iframe invisible?
if not, is there any other solution?

Old Pedant
07-16-2010, 08:14 PM
You know, Gizmo, that's a great idea.

You can even hide the <iframe>:

<iframe style="display: none;">

You can leave it visibile while you are developing the code and then hide it later.

No need for <pre> tags. You don't really care what it *looks* like. The line breaks will be there, but won't be visible in the browser. So what.

I don't see why you think this won't work in all browsers. Chrome handles <iframe> quite nicely. I use <iframe>s all the time--including even hidden iframes--and it all works with MSIE6/7/8, FF, Opera, Chrome, Safari.

Old Pedant
07-16-2010, 08:15 PM
I wish now that I'd thought of this for my answer in the other thread. DOH on me!!!

rnd me
07-16-2010, 08:32 PM
No need for <pre> tags. You don't really care what it *looks* like. The line breaks will be there, but won't be visible in the browser. So what.

sure about that?
run this IE8 developer tools test line:

document.body.innerHTML="1\n2\n3\n4"; alert(document.body.innerHTML)

pasted firefox result:

1
2
3
4

sketched ie result:

1 2 3 4





here is something that works the same in all browsers

function curl(U) {
var X = !window.XMLHttpRequest ?
new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest ;
X.open( 'GET', U, false); X.send('');
return X.responseText;
}


var myString = curl("test.txt");

alert( myString );

ajax also has the following benefits over an iframe:

no history entry
no click in ie7
safely work on files other than .txt
no extra window object to waste CPU and RAM
no status bar activity
no waiting on the iframe to load before attempting to get string, can call anytime
invisible, even with CSS disabled

Old Pedant
07-16-2010, 08:48 PM
Well, that's actually exactly the code I used in my other answer.

As demoed at http://www.MyWhizBang.com/simpleSearch.html

So, maybe I did the right thing, after all. <grin/>

Incidentally, this is *NOT* AJAX code. I guess it could be called "SJAX" ("Synchronous" as opposed to "Asynchronous") but since it's not XML just text, maybe it should be "SJAT"?? Hmmm...I think I like that: "Synchronous Javascript Access to Text".

********

I do have to play Devil's Advocate, a little:

Since what he wants is a set of *words*, he could presumably do

var text = [iframe contents].innerHTML;
var words = test.split(/\s+/g); // split on *any* white space

Clearly doesn't work if some of your "words" are actually multi word phrases.

Old Pedant
07-16-2010, 09:02 PM
By the by, I *did* double check, and indeed MSIE converts the \n to a true space.

I did txt.charCodeAt(1) and got 32.

So thanks, RndMe, for keeping me from rewriting code only to find it won't work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum