/    Sign up×
Community /Pin to ProfileBookmark

.txt file, any way possible?

All right, I not so much of an idiot to realize that javascript can’t directly access a .txt file. However, I am desperate to find anyway to somehow, indirectly access a .txt file from javascript. I don’t care if it is incredibly inpractical or if it requires like 5 other scripts in different languages, I just need to know if there is anyway at all that it is possible for javascript to indirectly access a .txt file.

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@mrhooNov 26.2006 — You can easily open and read any text file, whatever the extension, that is on the same server/ domain as the page in which the script is running, with javascript - but you can't save changes to the file on any server without some server side code.

Use the response text from an XMLHttpRequest to read same origin files.
Copy linkTweet thisAlerts:
@coppocksNov 26.2006 — You can "open" any text file with JavaScript as long as it's on the same computer as the JavaScript. But manipulating / "reading" the text is a different story.

If you use PHP for example (since you dont care about diff languages), then you could do something like this:

PHP <i>
</i>$lines = file('http://www.example.com/file.txt');
// then you "may" want to delimit the lines Array for your JavaScript Code
$lineCnt = count($lines);
for($j = 0; $j &lt; $lineCnt; $j++) {
if ($j == $lineCnt) {
$txt .= $line;
}
else {
$txt .= $line . '|';
//OR Newline if you prefer
//$txt .= $line. "n";
}
}

Then pass $txt to your JavaScript code via PHP:

JavaScript <i>
</i>&lt;Script Language=JavaScript&gt;
var retData = &lt;?=$txt;?&gt;;
var dispData = retData.split('|'); // Array
for(var i = 0; i &lt; dispData.length; i++) {
document.write(dispData[i] + "&lt;br&gt;");
}
&lt;/script&gt;


However, if you are using PHP, then there is no need for JavaScript in such a simple example because PHP can do this, unless of course, you have a special need to process text via Javascript, which you seem to have. One alternative to PHP is to use AJAX XMLHttpRequest and eliminate PHP completely.
Copy linkTweet thisAlerts:
@felgallNov 26.2006 — XMLHttpRequest is the only Javascript only solution for reading files from the server.
Copy linkTweet thisAlerts:
@tenniskid493authorNov 26.2006 — thank you for the help you offered...i realized that what I wanted to most likely cannot be achieved

I wanted to be able to add X # of gold to a persons account everytime they posted on a forum and I wanted it to be stored in a txt file, but the forum only allowed javascript coding. I guess I'll have to find some alternative way to do what I want to achieve.
Copy linkTweet thisAlerts:
@andkhlFeb 05.2007 — XMLHttpRequest is the only Javascript only solution for reading files from the server.[/QUOTE]

How do you do it? Would you show how it works using a simple example?

Example (conditions):

1) A .txt file and HTML file exist in the same directory on the server.

2) The HTML file has a form in it with a <div></div> tag and a button 'Show Me'. The .txt file has some text in it.

3) The user clicks on the button, his/her browser reloads, and the <div></div> tag displays the contents of the .txt file.

How do you do that?

Thank you.
Copy linkTweet thisAlerts:
@JMRKERFeb 06.2007 — Take a look at my post #3 on: http://www.webdeveloper.com/forum/showthread.php?t=137213

This may do what you want with JS only.

You will still need to split out the elements of the txt file you want to manipulate. Note: It will not save anything, only read and display.
Copy linkTweet thisAlerts:
@felgallFeb 06.2007 — If you just want the javascript to read the entire file then you don't need any PHP processing. Just retrieve the txt file directly instead of calling the PHP file from the Ajax request.
×

Success!

Help @tenniskid493 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...