Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Javascript: Write to txt file

Featured Replies

There is a text file on my server called "myfile.txt".

 

When I press a button on my main page, I want a javascript script to be executed, that writes "Hello world!" to the text file. If the file already has something in it, it creates a new line.

 

How would I do this?

Not with Javascript.

 

Edit: I should explain - Javascript = client side, not server side.

Edited by andyl

  • Author

Not with Javascript.

 

Edit: I should explain - Javascript = client side, not server side.

Ok, I'll rephrase the question:

 

 

There is a text file on my server called "myfile.txt".

 

When I press a button on my main page, I want some code to be executed, that writes "Hello world!" to the text file. If the file already has something in it, it creates a new line.

 

How would I do this?

  • Author

This is some javascript I have for reading a file, which works perfectly. Surely there must be a writing equivalent for this?

 

var filePath = "myfile.txt";
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET",filePath,false);
xmlhttp.send(null);
var fileContent = xmlhttp.responseText;
fileLines=fileContent.split("\r\n")

...which is the Javascript equivalent of you opening the file in your web browser - the web server allows read-only access to it from remote machines. Javascript can't write to the file because it runs in your browser, and writing to a remote file on a web server would be a huge security hole - if you could write what you want to it, anyone could write anything to it.

 

Writing to the file will need to be done with a server-side language, such as PHP.

But you can't execute PHP when pressing a button, right?

 

Of course you can. You press the button, it submits a form to a PHP script which does something with the data. How d'you think your post was submitted?

Of course you can. You press the button, it submits a form to a PHP script which does something with the data. How d'you think your post was submitted?

And if you're super-duper clever, you can make that happen asynchronously :ph34r:

Regardless of whether or not you feel he/she is capable of working with this, saying that what OP wants to achieve is not possible is not going to teach him/her anything.

Regardless of whether or not you feel he/she is capable of working with this, saying that what OP wants to achieve is not possible is not going to teach him/her anything.

Other than to use a more appropriate method, that is.

Regardless of whether or not you feel he/she is capable of working with this, saying that what OP wants to achieve is not possible is not going to teach him/her anything.

 

And installing a new server-side stack is a rational answer? Yes, Node is the new, trendy server-side choice, but the right choice to write to a file is PHP, because it's likely already installed.

Node is a completely different ballgame to client-side JavaScript. They may be based on the same language, but Node is extremely complex.

 

Compared to how simple it is to do this using PHP, there are probably hundreds of snippets on the interwebs, the logical thing to do here is to go the PHP route.

 

If the OP wants to learn Node that's fine, but personally, I've been making websites for 2 years and that page made me want to cry.

Node isn't complex at all. I may be saying that because I'm a lot more experienced than most of you, but I'm just saying, nobody else tried answering OP's actual question. PHP would be a simple solution, at the same time, this isn't what was asked--you never know, OP could have a bit of JS experience and is perhaps (for whatever reason) required to execute this in JavaScript.

 

A lot of subjective answers in here, and possibly, people who genuinely thought this was not possible in JS.

Also, node is just one of many options available to execute back end JS.

Node isn't complex at all. I may be saying that because I'm a lot more experienced than most of you, but I'm just saying, nobody else tried answering OP's actual question. PHP would be a simple solution, at the same time, this isn't what was asked--you never know, OP could have a bit of JS experience and is perhaps (for whatever reason) required to execute this in JavaScript.

 

A lot of subjective answers in here, and possibly, people who genuinely thought this was not possible in JS.

 

Of course Node is complex. All server side languages are complex. Relative to another server side language, it may not be, but in the grand scheme of things Node is a server side language and server side languages are complex and take a good while to learn. They are certainly much more difficult than client side scripting such as HTML/CSS. Nothing to do with your level of experience, that's a moot point and if I was you I'd be careful walking around saying you have more than others, it can come across as arrogant, even if that's not your intention, and even if it's true.

 

Further, if you read the thread, you'll see that the clarification between client side and server side JavaScript was made as early as possible. Nobody as far as I'm aware has said you can't do this with server side JS.

 

I also don't believe, and I'm sure the others will agree, it's always a case of straight up answering the OP question. Sometimes, and this is especially true of beginners and amateurs, they ask questions of things that are not possible or ideal. Guiding them in the direction of more suitable avenues is helpful - otherwise they can end up all over the shop.

 

It's a bit like somebody asking how to make a navigation bar in flash - it's no bad thing to advise them that doing it in CSS/HTML would be a better solution.

A lot of subjective answers in here, and possibly, people who genuinely thought this was not possible in JS.

Possible != sensible.

  • 7 years later...
On 5/29/2012 at 10:36 PM, Shaun said:

Regardless of whether or not you feel he/she is capable of working with this, saying that what OP wants to achieve is not possible is not going to teach him/her anything.

This was many years ago, but I came across it today and just had to say - well said :)

  • Jo 90 locked this topic
Guest
This topic is now closed to further replies.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.