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
If you could change the words.txt file to look like word1,word2,word3 using comma as a delimiter you could seperate the data and replace the commas with line breaks.
Hmmm I must leave the words in the txt file with line breaks, I've already thought about the word1,word2,word3 form but it's not goot for me. But my main problem is that I don't know how to read an external txt file.. And by the way when I think of it doesn't matter if the data word1 word2 word3 will be in an array or in a variable.. Thanks, Ran
Javascript can't access file IO, so doing this is impossible--you should look into parsing this server-side (PHP/ASP/etc.), then handling the data on the client side (Javascript) Actually, thinking about it now--it is possible but I wouldn't recommend it. I believe there's an IE specific Java-extension in Javascript that would allow this, but it would only work in IE. You'd have to create java.io object and use that--but you wouldn't include anyone using IE or people who don't have Java enabled (different from JavaScript).
Yes, you would have to use a SSL (PHP ASP CF) to read the data from a text file. If you want the variable straight in javascript you can either use AJAX to get the data (.responseText) or you can get the variable through the SSL and populate a hidden text field with it to be read by javascript via getElementById('field'); I recommend using AJAX though because it is cleaner.
javascript CAN easily access file using small hidden signed java applet. http://www.youtube.com/watch?v=wqpwyAAhKGg
I recommend transforming your .txt file in an xml format and then read it with javascript via xmldom!
Convert your txtx file into a JSON format file. {"word1","word2","word3"} And use jQuery to get the data